DEV Community staff staff

Posted on

Daily Challenge #146 - The Dollar Game


You are given a list of people and their current net worth in U.S. Dollars. Some people might have a positive net worth, others could have a negative net worth (i.e. are in debt), while some are just broke.

There are many friendships across these financial groups. No single person is alone typically, you can draw a friendship line from one person to any other person through x number of people. In this community, each person gives 1 dollar to each of their friends. Additionally, each person can take 1 dollar from each of their friends.

Who should take or give a dollar so that everyone has a non-negative balance?


Your function giveAndTake will pass two arguments

  • people - a hash object with names as keys and net worth as value.
  • friendships - an array of pairs of names that are friends.


Your function should return an array of 'moves'. Each 'move' is an array of two elements: The name of a person and the value 1 or -1. 1 indicates they should take a dollar from each friend. -1 indicates they should give a dollar to each friend.

For example: [['David',1], ['Brian',-1], ['Alice',-1], ['Charlie',1]]


people: {Adam: 1, Bob: -1, Carol: -2, Donna: 2, Eddie: 3}
friendships: [['Adam','Carol'],['Adam','Eddie'],['Bob','Carol'],['Carol','Eddie'],['Donna','Eddie']] 

people: {Adam: -4, Bob: 1, Carol: 2, Donna: 2, Eddie: 0}
friendships: [['Adam','Donna'],['Adam','Carol'],['Bob','Carol'],['Carol','Eddie'],['Donna','Eddie']]

people:{Adam: 0, Bob: 2, Carol: -2, Donna: 3, Eddie: -2}

Good luck!

This challenge comes from hufftheweevil on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email with your suggestions!

Top comments (2)

idanarye profile image
Idan Arye • Edited

Are people allowed to bring themselves and/or their friends into debt? If they are allowed, then the order of moves is not important - only the final number times each person given minus the final number of times they have taken.

souksyp profile image
Souk Syp. • Edited

My attempt... There is no givers in my cruel world. Just steal/take from friends.