Like most junior developers, I am looking for a job and looking for a job includes a lot of code challenges. Now, technically, after I made fun of the last code challenge I did, it did come up in an interview for a FAANG company. SO I may be jinxing myself.

But this code challenge was a pretty practical example. It also asked me specifically to use Ruby since that’s what the stack of the company would be. For not-suing-me-sake, some variables are changed.

Given an array of people we have to pay with an amount of money that would be sent to a popular payment API like Stripe or Braintree, organize each payment into a separate group so that each transaction can be grouped with another transaction from a different person, but not two of the same. 

Also take in as an optional argument a maximum group size for a group of payments in case we need that with a default size of 100.

It seemed simple enough but it turned out to be a little more complex than I imagined. To begin we have something that looks like this:

module PayPeople

end

And we know we’re going to need something that pays people inside that module and that takes in an array and an optional max_group_size.

So now we have something like:

module PayPeople

  def self.group_payments(pay_array, max_group_size=100)
  end
end

Progress! But we still haven’t done anything with our arguments.

Aside: we use the _self _keyword since this is a Ruby module, a collection of methods we can call that belong to a single file but that we can call throughout our application with some import syntax.

Well I like to first think about what I’m going to return at the end of my function when thinking about a problem, which will be an array of arrays, since we are breaking a list of payments into different groups. I know there will be at least one group in there, assuming we are dealing with some payments. So I will assign my return variable like so.

#ruby #dogs #interview-questions #programming

How to Pay People In Ruby!
1.30 GEEK