Count Sub Islands

Asked atFreshwoks
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. 01Traverse every land component in grid2.
  2. 02DFS or BFS its four-directional cells.
  3. 03Mark invalid if any visited cell is water in grid1.
  4. 04Always finish visiting the component.
  5. 05Increment only if the final flag remains valid.

Reference answer

Then expect these follow-ups

  • How would you avoid recursion-depth risk?

    Tests: follow-up reasoning

  • What changes if grid2 may be mutated?

    Tests: constraint adaptation

Hiring now

Freshwoks has 111 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.
  • Head of Solution Engineering, Americas

    San Mateo, CA, United States

  • Lead - Solution Engineer

    Remote

  • Senior Manager - Sales (Expansion) (m/f/d)

    Berlin, Germany

Or practice this question first

Next question