juan.rosero.dev
October 26, 2026

The shifting problem

#algorithms-and-data-structures#software-development

Hey again! The other day I failed a technical interview that tested my algorithm knowledge. As a good stoic, I decided to take it as an opportunity to improve my knowledge. Therefore, I’m starting this series of smaller sized posts about algorithms and the reason why they are important. Hopefully with some easy to understand examples that can illustrate how they are useful.

To start, I have to say that this algorithm knowledge is like a toolbox. You just get to know other ways of solving a problem and that becomes a new tool in your toolset. Unlike real tools though, the utility of these data structures or techniques is not clear from the start, so I’ll try to place you in a hypothetical scenario that might reveal their utility.

A very messy hospital

Imagine that you’re a doctor. You are in charge of this emergency room and your goal is to save as many lives as possible. You get some patients with mild symptoms and others that are close to death, so you need a way of prioritizing who gets attention first.

You are pretty smart and you don’t want to make this complicated for your patients so you come up with this solution of a line of chairs. The chair closest to your door gets attention first and every time you attend a patient people simply move up one position. The system works well but you realize that every time a badly hurt patient arrives, he needs to be placed at the start of the line so everybody needs to move back one chair.

A critically hurt patient, number 31, is placed in the first chair, next to the doctor, and every other patient shifts one seat back

That doesn’t seem to be a problem until a very busy day arrives, with hundreds of patients, most of them badly hurt. That chair position mechanism that created simplicity now is slowing everything down.

After a hard day you get back home and try to give it another crack. Why is this system so slow? How can you make it faster? Hypothetical people are going to die if you don’t get this right.

The clear flaw of this system is the shifting that needs to happen every time the list is re-ordered, either when someone is placed or removed from the front. So how do you stop this shifting?

Here is your card

Next day, you introduce a card system. Everybody that gets to the emergency room gets a card with two numbers: one for themselves and one for the patient in front of them. The second number is written with pencil, so you can change it if you need to do so.

The next patient to get attention would just be missing its “front” number.

Two patient cards: patient 31 has an empty "in front" number and is next to be seen, while the "in front" number of patient 07 is 31, pointing to the card ahead of it

Now, pause at this point and think, what would happen in this new system when someone arrives and is badly hurt?

Well, if you haven’t got to this conclusion already, the result is that the shifting is gone. Every time you change the “In Front” numbers in any of the cards, the order of the rest changes automatically.

Now, if you someone arrives and it’s badly hurt, you just give him a card with no “In Front” number and update the previously frontliner card to that new’s patient’s number.

Great job! Now you can focus on giving the best care possible.

Back to computers

I want you to remember that shifting problem in the future because it is the sort of problem that a linked list solves. Each one of those cards is an element on that list and the “front” number is simply the pointer to the element in front of it.

Back at my interview, I was asked to create an LRU (Least Recently Used) Cache. When I read: “every time a resource is used, you need to place it at the front of the line” my mind thought: “I can fix that with an array.push right?”. Of course, pushing to that array was cheap and allowed me to keep track of the position of my resources, but when I needed to re-order them, an expensive operation was involved, as I had to shift everybody up or down.

The cards in my example had only two numbers, but if you need to have more pointers (like one for who’s next) you can easily add that to your program. The important bit, again, is that every time a new member arrives, you don’t need to shift the whole list once again.

Conclusion

I didn’t get the job, but I got to remember a valuable lesson and I’m now motivated to keep writing about these algorithms and tools.

I hope, on the other hand, that this story creates that “fear of shifting all the elements in a list” that I believe is the basis for recognizing when this tool can be used to solve the problem.

Keep in mind that a real emergency room would also need to place patients in the middle of the line, depending on how hurt they are. The cards still save you the shifting there, but you would have to walk the line to find the right spot.

Thanks again for reading and see you next time!