Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Problem: Houses are arranged in a circle with values [2, 3, 2, 5, 1]. You may rob any set of houses with no two adjacent in the circle (the first and last are adjacent). What is the maximum total value?
- A. 5
- B. 13
- C. 7
- D. 8
Correct answer: D. 8
Run linear house robber twice, on [2, 3, 2, 5] and on [3, 2, 5, 1], and take the larger: 3 + 5 = 8. Taking every other house from index 0 (2 + 2 + 1, where 1 is next to the first 2 anyway) gives 5, 13 ignores adjacency, and forcing house 0 into the plan gives 2 + 5 = 7.
More Coding Interview Problems questions
- Problem: Count the subsets of [3, 1, 4, 2, 6, 4] whose sum is exactly half the total sum (the subset may be any size; the empty subset is…
- Problem: Count the longest increasing subsequences (subsequences of maximum length, strictly increasing) of [1, 3, 5, 4, 7]. Two index…
- Problem: Snakes and ladders on squares 1–30: from square s, a die roll of r moves you to s + r (for 1 ≤ r ≤ 6, if s + r ≤ 30). Ladders…
- Problem: Nim with three piles of sizes 1, 2 and 3. A move removes any positive number of stones from one pile. The player who takes the…
- Problem: How many subarrays of [1, 2, 3, 0, 3, 2, 1] have a XOR of zero?
- Problem: How many integers from 1 to 50 have exactly two 1-bits in their binary representation?