</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Grid and Simulation

What is this?

These are problems with no clever algorithm to find โ€” you just walk through a grid or follow a set of rules step by step, exactly as written. The skill is bookkeeping: keep the right counters and sets, update them as you go, and never let your simulation drift away from what the rules actually say. Get the details right and the answer falls out on its own.

flowchart TD A["Read the rules literally"] --> B["Pick state to track"] B --> C["Counters or sets or rolling row"] C --> D["Update on each step"] D --> E["Read off the answer"]

๐Ÿ’ก Fun fact: Tic-Tac-Toe has only 765 truly distinct board positions once you remove rotations and reflections โ€” small enough that a perfect player can be hardcoded, which is exactly why the interview version asks you to design O(1) win checks instead of brute-forcing the board.

๐Ÿ”“ The 4 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: some problems do not reward a clever trick โ€” they reward faithfully modeling the rules and walking the data exactly as described. The skill is bookkeeping: pick the right counters and sets, update them as you scan, and never let the simulation drift from the spec.

The pattern

Every problem here is solved by reading the rules literally and tracking just enough state to answer the question in one pass. The win comes from choosing what to store, not from a fancier algorithm.

  • Build row from previous row. When each line depends only on the one above it, keep a single array and derive the next from it, rather than recomputing everything from scratch each time.
  • Incremental counters instead of full scans. Maintain per-row, per-column, and per-diagonal tallies that you bump on each move, so you can detect a winning or invalid state in O(1) instead of re-scanning the whole board.
  • Sets for "seen already." A grid with uniqueness constraints (rows, columns, sub-boxes) is just three families of sets; a value that fails to insert is a duplicate.
  • Bound the work with math. When you must enumerate divisors or factors, walk only up to the square root and pair each divisor with its complement, turning O(n) into O(sqrt n).

The common thread: define your state up front, decide how each step mutates it, and trust the loop.

The problems

Pascal's Triangle โ€” build each row from the previous one, since every interior entry is the sum of the two values above it. Keep only the last row and extend it; no full grid needed.

Design Tic-Tac-Toe โ€” instead of scanning the board after every move, keep per-row and per-column counters plus two diagonal counters. A player who pushes any counter to the board size has just won, checked in O(1) per move.

Valid Sudoku โ€” track a set for each row, each column, and each 3x3 box. Scan the board once; the moment a digit fails to insert into its row, column, or box set, the board is invalid.

Perfect Number โ€” sum the proper divisors by iterating only up to the square root, adding both i and n / i for each divisor pair, then comparing the total to the number itself.

Key takeaways

  1. Simulation problems reward faithful modeling over cleverness โ€” read the rules and follow them exactly.
  2. Choose your state deliberately: counters, sets, or a single rolling row, matched to what the question asks.
  3. Incremental counters turn repeated full-board scans into O(1) checks per step.
  4. Sets make uniqueness constraints trivial โ€” a failed insert is a duplicate.
  5. Bound enumeration with the square root and divisor pairing to keep brute force fast.

Core idea: divisors of n come in pairs (i, n // i) that straddle โˆšn โ€” one factor below the square root, its partner above. So to sum all divisors you only ever have to scan up to โˆšn and add both members of each pair you find. That single observation turns an O(n) divisor walk into O(โˆšn).

Problem, rephrased

A perfect number is a positive integer that equals the sum of its proper divisors โ€” every positive divisor except the number itself. The classic example is 28: its proper divisors are 1, 2, 4, 7, 14, and 1 + 2 + 4 + 7 + 14 = 28. Given an integer n, return True if it's perfect and False otherwise.

Picture lining up every number that divides cleanly into n, crossing off n itself, and adding what's left. If the total lands exactly back on n, you've found a perfect number. Overshoot or undershoot, and it isn't.

n Proper divisors Sum Perfect?
6 1, 2, 3 6 True
28 1, 2, 4, 7, 14 28 True
12 1, 2, 3, 4, 6 16 False (abundant)
8 1, 2, 4 7 False (deficient)
1 (none) 0 False
496 1,2,4,8,16,31,62,124,248 496 True

Notice the second-to-last row: 1 has no proper divisors at all (its only divisor is itself), so its sum is 0 โ‰  1. And the perfect numbers are rare โ€” 6, 28, 496, 8128, โ€ฆ โ€” sandwiched between abundant numbers (sum overshoots, like 12) and deficient ones (sum undershoots, like 8).

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.