Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: Children stand in a line with ratings [1, 0, 2]. Each child gets at least one candy, and a child with a higher rating than a neighbour gets more candies than that neighbour. What is the minimum total number of candies?

  1. A. 6
  2. B. 4
  3. C. 7
  4. D. 5

Correct answer: D. 5

Two passes: left-to-right enforce the rule against the left neighbour, right-to-left against the right neighbour. A single pass misses constraints from the right.

More Coding Interview Problems questions

Exams that ask this topic