Supermarket Queue

Typical weekday after work

This kata made it to my highlight, because it presented a kind of problem that feels very easy to a human mind, but tricky to solve on a computer.

The kata basically asks you to give the time required for a set of check out counters to finish a fixed queue. These things naturally sprung to my mind when I consider a typical supermarket scenario:

Customers at the front of the queue would move to the first available counter.

Each and every customer in the queue would require different time to finish check out. Some might just want to buy a soda or a Mars bar. Some have four mouths to feed in the family, and got a month worth of taters to last the snow storm season.

That means the queue is more likely to flow after counters that are handling individual Mars bars and sodas.

Though of course, in this hypothetical scenario, the queue doesn’t change. No one is going to decide to drop out of the queue because they forgot bread, nor would a customer with a trolley full of apocalypse preparations let the Mars bar guy behind them to go first, out of politeness.

WARNING: I try not to spoil the actual solution in this article, but I might accidentally slip hints towards the correct answer. If you want to attempt this kata on your own first, stop right here and do that now!

I mentioned in my first Wombo Combo that I suck at maths. Therefore I approached this kata with the trail of thoughts I listed above. In my mind I would project a bird’s eye view of a supermarket checkout in my head, where customers of different purchases flow like water. I just need to get the time it would take this supermarket to finish that queue (assuming no one else is coming). All good.

The start of my code design went something akin to game development. I had an array of checkout counters, and an array of customers in queue. For each checkout counter that doesn’t have a customer attached, I pop the first customer off the queue array and add them to the first available counter. Each customer’s required time to complete check out is reduced by 1 on every loop as long as they are in the checkout array. Once that time hits 0, the checkout counter is ready to take a new customer, whose time will reduce on every loop by 1 again.

It was something like: for each checkout counter, if it doesn’t have a customer, or the checkout time is 0, pop the first one in. If it has a customer, reduce its checkout time by 1.

Sounds fine, right? Well when I hit the Run button, it passed the test where there’s no customer in queue. It passed the second test too with two customers.

Then I got a timeout error.

The for-loop took too long to process some tests that have long checkout time, or a lot of customers in the queue. I found out from this kata that Codewars has a maximum stall limit of 2 minutes. Considering the nature of katas, certainly no script is supposed to run for that long.

It was the only JavaScript kata that I could not complete so far and had to skip to the solutions. As most things go, the solution is stupidly simple. Everything that I’ve said above, are trails of how I have overcomplicated the problem.

If you have read this far and haven’t attempted the kata yet, I highly recommend giving it a go, or even just to look at the solution so you see what I mean.