House Robber problem (something like this)

Asked atAmazon
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Define take and skip choices at each position
  2. 02Use dp[i] = max(dp[i - 1], value[i] + dp[i - 2]) for the linear version
  3. 03Initialize empty and one-element inputs explicitly
  4. 04Compress to previous two states
  5. 05For the circular version, solve the two cases excluding first or last and take the better answer

Reference answer

Then expect these follow-ups

  • What constraint would change your chosen approach?

    Tests: constraint adaptation

  • How would you test the edge cases before implementation?

    Tests: implementation extension

Hiring now

Software engineer roles are open right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Senior Software Engineer -.NET

    Tyler Technologies · Annapolis, Maryland

  • Software Development Engineer II

    SaaS Labs · Bengaluru, Karnataka, India

  • Advanced Software Engineer - Product Architect (Legal, Safety, and Risk)

    Metropolitan Transportation Authority · New York, NY

Or practice this question first

Next question