Number of Students Unable to Eat Lunch - Problem

Welcome to the school cafeteria lunch rush! 🍽️

The cafeteria serves two types of sandwiches: circular sandwiches (0) and square sandwiches (1). Students are lined up in a queue, each with their own preference for either circular or square sandwiches.

Here's how the lunch process works:

  • πŸ₯ͺ Sandwiches are placed in a stack (top sandwich served first)
  • πŸ‘₯ Students wait in a queue (front student gets served first)
  • βœ… If the front student likes the top sandwich β†’ they take it and leave
  • ❌ If the front student doesn't like the top sandwich β†’ they go to the back of the queue

The process continues until no remaining students want the current top sandwich, meaning some students will go hungry! 😒

Your task: Given the sandwich stack and student preferences, determine how many students will be unable to eat lunch.

Input: Two arrays - students[] (preferences in queue order) and sandwiches[] (sandwich types in stack order)

Output: Number of students who cannot get their preferred sandwich

Input & Output

example_1.py β€” Basic Case
$ Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
β€Ί Output: 0
πŸ’‘ Note: Front student (wants square) can't take circular sandwich, so goes to back. Queue becomes [1,0,0,1]. Next student (wants circular) takes it! Queue: [0,0,1], Stack: [1,0,1]. Continue this process: all students eventually get fed, so 0 remain hungry.
example_2.py β€” Some Students Unfed
$ Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
β€Ί Output: 3
πŸ’‘ Note: We have 4 students wanting square sandwiches and 2 wanting circular. The sandwich stack starts with 1 square, then 3 circular, then 2 square. After serving 1 square + 2 circular + 1 more square (when we get back to it), we have 3 students left wanting square sandwiches but only circular ones remaining in the stack.
example_3.py β€” Edge Case Single Student
$ Input: students = [1], sandwiches = [0]
β€Ί Output: 1
πŸ’‘ Note: The single student wants a square sandwich (1) but the only sandwich available is circular (0). Since they don't match, the student cannot eat and remains hungry. Hence, 1 student is unable to eat.

Visualization

Tap to expand
Cafeteria Sandwich DistributionStudents010Wants: πŸŸ‘Γ—2, πŸŸ©Γ—1Sandwich Stack (Topβ†’Bottom)🟩Match! Feed 1 student🟑Match! Feed 1 student🟑Match! Feed 1 student🟩No students want this!🟑Never reachedResultUnfed Students: 0🟑 remaining: 0🟩 remaining: 0All students fed! βœ“πŸ’‘ Key Insight: Order of students doesn't matter, only the counts!We process sandwiches in stack order and match against available student counts
Understanding the Visualization
1
Count Preferences
Count how many students want circular (0) vs square (1) sandwiches
2
Process Stack Order
Go through sandwiches from top of stack (first served)
3
Match and Feed
If students want current sandwich type, feed one (decrement count)
4
Stop at Blockage
When no students want current sandwich, remaining students go hungry
Key Takeaway
🎯 Key Insight: The breakthrough realization is that we don't need to simulate the queue rotation at all! Since students will keep rotating until they find their preferred sandwich (or until it's impossible), we only need to track how many students want each type and process the sandwich stack in order.

Time & Space Complexity

Time Complexity
⏱️
O(n)

We make one pass to count students (O(n)) and one pass through sandwiches (O(n)), giving us O(n) total.

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

We only need two counters for the two types of sandwiches, regardless of input size.

n
2n
βœ“ Linear Space

Constraints

  • 1 ≀ students.length, sandwiches.length ≀ 100
  • students.length == sandwiches.length
  • students[i] is 0 or 1, where 0 means circular sandwich preference
  • sandwiches[i] is 0 or 1, where 1 means square sandwich
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
28.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen