Data Structures & AlgorithmsVery hardGoogle · Flipkart interview
Problem: Run Conway's Game of Life on an unbounded board (a live cell survives with 2 or 3 live neighbours; a dead cell becomes live with exactly 3).
Problem: Run Conway's Game of Life on an unbounded board (a live cell survives with 2 or 3 live neighbours; a dead cell becomes live with exactly 3).
from collections import Counter
def step(live):
c = Counter((r + dr, q + dq) for r, q in live
for dr in (-1, 0, 1) for dq in (-1, 0, 1) if dr or dq)
return {p for p, k in c.items() if k == 3 or (k == 2 and p in live)}
live = {(0, 1), (1, 2), (2, 0), (2, 1), (2, 2), (5, 5), (5, 6), (6, 5)}
for _ in range(4): live = step(live)
print(len(live))What is printed?- A. 9
- B. 8
- C. 10
- D. 5
Correct answer: A. 9
The first five cells form a glider, which keeps 5 live cells and simply moves one cell diagonally every 4 generations. The L-shaped triomino at (5,5) becomes a 2 × 2 block (a still life) after one step. Over 4 steps the two do not interact, so 5 + 4 = 9.
More Coding Interview Problems questions
- In the Tower of Hanoi with 5 discs and 3 pegs A, B, C in a row, moves are allowed only between ADJACENT pegs (A↔B, B↔C, never A↔C…
- Problem: Count the integers in [1, 1000] that are coprime to 1000. python from math import gcd print(sum(1 for i in range(1, 1001) if…
- Reservoir sampling keeps k = 2 items from a stream of n = 10 items: the first 2 fill the reservoir, and item i (i > 2) is admitted with…
- Problem: An LFU cache (capacity 2) evicts the least frequently used key; ties are broken by least recently used. get returns −1 on a miss…
- Edge case: A generator expression is consumed twice. python g = (x x for x in range(4)) print(sum(g), sum(g)) What is printed?
- Problem: Sort students by grade (descending), breaking ties by name (ascending), using two passes of Python's STABLE sort. python s =…