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?
- A. 6
- B. 4
- C. 7
- 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
- Problem: Find the maximum amount of water held between two vertical lines of heights [1, 8, 6, 2, 5, 4, 8, 3, 7] (the water is limited by…
- Problem: What is the largest integer x with x² ≤ 2,000,000,000 (the floor of the square root of 2 × 10⁹)?
- Problem: What is the sum of the first 50 odd positive integers (1 + 3 + 5 + … + 99)?
- Problem: In a binary-tree-free DFS on the undirected graph with edges 0-1, 0-2, 1-3, 2-4, 3-4, 4-5, starting from vertex 0 and always…
- Problem: What does the following print? python print(sorted(['b', 'B', 'a', 'A']))
- Problem: Selection sort is run on [4, 3, 2, 1] in the usual way (for each position, find the minimum of the unsorted suffix and swap it…