FastPrepMaximum Loot on a Line or Circle
Problems/Maximum Loot on a Line or Circle

Maximum Loot on a Line or Circle

Datadog logoDatadog● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names House Robber and its circular follow-up. This exercise covers both with one street-shape flag. Empty input, examples, bounds, and the callable interface are specified for practice. The core task matches the report at about 90%.

Each entry in money is the amount held by one house, in street order. Choose houses with the largest possible total, without choosing two neighboring houses.

When circular is false, the houses form a line. When it is true, the first and last houses are also neighbors if there are at least two houses. A single house does not conflict with itself. You may choose no houses. Return the maximum total; an empty street returns 0.

Function

maxNonAdjacentLoot(money: int[], circular: boolean) → int

Examples

Example 1

money = [4,1,2,9]circular = falsereturn = 13

Choose the first and fourth houses, worth 4 + 9 = 13. They are not neighbors in a line. No valid choice has a larger total.

Example 2

money = [4,1,2,9]circular = truereturn = 10

The first and fourth houses are neighbors in a circle, so they cannot both be chosen. Choosing the second and fourth houses gives 1 + 9 = 10, the maximum.

Example 3

money = [9]circular = truereturn = 9

There is only one house. It does not conflict with itself, so choosing it gives 9.

Constraints

  • 0 <= money.length <= 5000
  • 0 <= money[i] <= 100000
  • circular is a boolean.

More Datadog problems

  • Query Log MatchPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Delete a Filesystem TreePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Grouped-Query Attention Forward PassPHONE SCREEN · Seen Jun 2026
All Datadog interview questionsSee Datadog hiring insights
Saved
public int maxNonAdjacentLoot(int[] money, boolean circular) {
    // Write your code here.
}
Checking account…