Find Matching Runs, Clear Them Once, and Apply Gravity
Quick Overview
Detect maximal horizontal and vertical matching runs, clear their union once, and apply stable downward gravity. Preserve overlapping matches and distinguish the single-pass task from cascading elimination.
Find Matching Runs, Clear Them Once, and Apply Gravity
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Given an m-by-n grid, find every horizontal and vertical run of at least three equal values. Report a run as[value,length]; for example, four consecutive9 values form[9,4]. Then clear every matched cell to0 and move remaining nonzero values downward within each column.
### Constraints and Clarifications
This is a single detection, clearing, and gravity pass, not repeated elimination until stable. For a precise practice interface, treat0 as empty and exclude it from matching, report maximal runs rather than overlapping length-three windows, and return both the run list and final grid. Enumerate horizontal runs by row then starting column, followed by vertical runs by column then starting row. These output and empty-cell conventions are explicit normalization. Identical[value,length] entries from different runs remain separate. No numerical size bounds are supplied; an empty grid returns empty results.
### Part 1 — Detect Runs
Find qualifying runs on the original grid in both directions.
#### What This Part Should Cover
Do not erase cells during detection, since a horizontal and vertical match can overlap.
### Part 2 — Clear and Drop
Clear the union of matched cells and compact each column downward while preserving the vertical order of survivors.
#### What This Part Should Cover
Use a separate mark structure and a bottom-up write pointer, and do not detect new matches created by gravity in this one-pass variant.
### What a Strong Answer Covers
Correct maximal-run boundaries, overlap handling, deterministic reporting, stable column compaction, and linear complexity.
### Follow-up Questions
- Why can clearing a horizontal run immediately cause a vertical match to be missed?
- What extra loop would be needed for a repeated-cascade variant?
Overview: Detect maximal horizontal and vertical matching runs, clear their union once, and apply stable downward gravity. Preserve overlapping matches and distinguish the single-pass task from cascading elimination.
Given an m-by-n grid, find every horizontal and vertical run of at least three equal values. Report a run as[value,length]; for example, four consecutive9 values form[9,4]. Then clear every matched cell to0 and move remaining nonzero values downward within each column.
Constraints and Clarifications
This is a single detection, clearing, and gravity pass, not repeated elimination until stable. For a precise practice interface, treat0 as empty and exclude it from matching, report maximal runs rather than overlapping length-three windows, and return both the run list and final grid. Enumerate horizontal runs by row then starting column, followed by vertical runs by column then starting row. These output and empty-cell conventions are explicit normalization. Identical[value,length] entries from different runs remain separate. No numerical size bounds are supplied; an empty grid returns empty results.
Part 1 — Detect Runs
Find qualifying runs on the original grid in both directions.
What This Part Should Cover Guidance
Do not erase cells during detection, since a horizontal and vertical match can overlap.
Part 2 — Clear and Drop
Clear the union of matched cells and compact each column downward while preserving the vertical order of survivors.
What This Part Should Cover Guidance
Use a separate mark structure and a bottom-up write pointer, and do not detect new matches created by gravity in this one-pass variant.
What a Strong Answer Covers Guidance
Correct maximal-run boundaries, overlap handling, deterministic reporting, stable column compaction, and linear complexity.
Follow-up Questions Guidance
Why can clearing a horizontal run immediately cause a vertical match to be missed?
What extra loop would be needed for a repeated-cascade variant?