Counting Valid Push and Pop Logs for a Docs Undo Stack
Last updated: November 29, 2026
Quick Overview
A Google Docs undo buffer records n push and n pop operations. Count how many operation logs are valid (never pop an empty stack and end empty), and find the probability that a uniformly random arrangement of the n pushes and n pops is valid.
0
0
0 solved
A Google Docs undo buffer records n push and n pop operations. Count how many operation logs are valid (never pop an empty stack and end empty), and find the probability that a uniformly random arrangement of the n pushes and n pops is valid.
Catalan numbers show up all over Google questions (balanced parentheses, tree shapes, stack sequences). The interviewer wants the bijection to Dyck paths, the reflection argument for the closed form, and a quick brute force to confirm.
What the Interviewer Expects
- Map valid logs to Dyck paths
- Derive the count with the reflection principle
- Compute the probability of validity
- Verify with brute force for small n
- Name other structures counted by Catalan numbers
Key Topics to Cover
How to Approach This
- Define your hypotheses (H0 and H1) clearly before performing any test.
- Calculate required sample size BEFORE running an experiment, using power analysis.
- Remember the Central Limit Theorem: sample means become approximately normal with large n.
- Watch for Simpson's paradox. Always segment data by key dimensions.
- Distinguish between statistical significance and practical significance.
Possible Follow-up Questions
- How many valid logs have their stack empty only at the end?
- How does the count change if the stack has a maximum depth of 2?
- How would you sample a uniformly random valid log?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Browse Statistics QuestionsSample Answer
Clarify
A log is a sequence of pushes and pops. It is valid if every prefix has at least as many pushes as pops. Under the random model, all arrangements are equally likely....
Approach
Draw each push as an up step and each pop as a down step. A valid log is a path from height 0 to 0 that never goes below 0, a Dyck path. Count invalid paths: each one first touches at some step; ...