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?

  1. A. 5
  2. B. 13
  3. C. 7
  4. 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

Exams that ask this topic