GrammarlyRemove Adjacent Duplicates in String (LC 1047 → LC 1209)
LC 1047 (repeatedly remove adjacent duplicate pairs) followed by LC 1209 (remove k adjacent duplicates), both unchanged LeetCode originals on HackerRank. Some screens add a third string-dedup step with mixed letters and digits plus a window size, or open with LC 56 before the chain.
Problem
A chained phone-screen sequence on HackerRank. Candidates confirm both parts are unchanged LeetCode originals.
- Part 1 — LC 1047: given a string, repeatedly remove pairs of adjacent equal characters until no such pair remains; return the final string.
- Part 2 — LC 1209 (follow-up): the same idea, but a removal happens when
kadjacent equal characters line up; repeat until none remain.
Examples
axxazx→zx(Part 1)
Variants
Four-question screen
One one-hour screen opened with Merge Intervals (LC 56), then ran 1047 → 1209 → a third string-dedup step whose input mixes letters and digits and adds a window size. The exact rules of that third step were not fully recalled; explaining the approach and walking an example was enough to pass.
Three-question screen
1047 → 1209 → a binary-tree question where the input is a plain list of numbers and the tree must be built first (Sum of Left Leaves, LC 404).
Pitfalls
- The recruiter may not say how many questions are coming; four questions in one hour has happened. Do not over-polish Part 1.
- Part 1 is usually solved in minutes — the evaluation is mostly on how cleanly the stack idea carries over to
k. - The third step's spec is loose; clarify how digits are treated and what the window size bounds before coding.
Follow-ups
Preparation
- Write LC 1047 and LC 1209 back to back in under 15 minutes, reusing the same stack skeleton.
- Re-derive the
(char, count)stack from the pair-only version out loud, as you would when the follow-up lands. - Do a timed one-hour set: LC 56 + LC 1047 + LC 1209, leaving 10 minutes for an unknown fourth question.
