Number of students unable to get preferred tiffin
Filed under Microsoft
Free read 3 of 3Understand the matching rules between the student queue and tiffin stack.
The question
You are given a stack of tiffins represented by binary digits and a queue of students wanting specific tiffins. If the student at the front matches the top tiffin, both are removed; otherwise, the student moves to the back of the queue. This process stops when no students can take a tiffin. Return the number of students unable to eat.
Constraints
- 1 <= students.length, tiffins.length <= 100
- tiffin and student values are either 0 or 1
Examples
- Input
- students = [1,1,0,0], tiffins = [0,1,0,1]
- Output
- 0
- Why
- Every student eventually gets their preferred tiffin.
Approach
Understand the matching rules between the student queue and tiffin stack.
Simulate the queue and stack operations directly using collections until no progress is made.
Count the total preferences for 0 and 1, then decrement counts as tiffins are processed until a mismatch occurs.
All students want the same preference but available tiffins are the opposite.
Tips
- What they are testing: Test cases where no students can be served, leading to an infinite loop if not handled by tracking consecutive failures.
- Common mistake: Failing to break the simulation loop when a full circle passes without any student being able to take a tiffin.
Related Microsoft questions
Collected from public candidate posts and coding forums. Updated Nov 2026.