Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: Min-cost climbing: cost = [10, 15, 20, 5, 1]. You may start at index 0 or 1, and from index i you can pay cost[i] to move up 1 or 2 steps. Reaching the top means going past the last index. What is the minimum total cost?
- A. 20
- B. 30
- C. 21
- D. 31
Correct answer: A. 20
dp[i] = min(dp[i − 1] + cost[i − 1], dp[i − 2] + cost[i − 2]): start at index 1 (15), jump to index 3 (5) and then past the end, total 20. Forcing a start at index 0 gives 30, requiring to land on the last index gives 21, and greedily stepping to the cheaper next stair gives 31.
More Coding Interview Problems questions
- Problem: Count the sequences of steps from {1, 2, 5} that sum to exactly 10 (the order of steps matters).
- Problem: A robot starts at (0, 0) facing north on an unbounded grid. Instructions: 'G' moves one step forward; 'L' turns 90° left; 'R'…
- Problem: Elevator starts at floor 0. Requests arrive in order for floors 3, 1, 4 (the elevator serves each request by going directly to…
- Problem: A traffic light cycles green for 30 s, then yellow for 5 s, then red for 25 s, starting at time 0 with green. How many whole…
- Problem: A water-jug problem: a 3-litre jug and a 5-litre jug, both empty, and unlimited water. Allowed actions: fill a jug fully, empty a…
- Problem: The string "PAYPALISHIRING" is written in a zigzag on 3 rows (letters go down the rows in a zigzag, then the rows are read left…