← All questions
MediumCoding

House Robber II

Asked atAcko
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

Acko has 11 open roles 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.
  • Manager - Finance

    Bengaluru, Karnataka, India

  • Sr Associate - Health claims (CL&MR)

    Bengaluru, Karnataka, India

  • Customer Health Solutions Expert - Health Claims

    Bengaluru, Karnataka, India

Or practice this question first

Next question