Klarna interview question

Write an algorithm to solve this puzzle.