Skip to main content

Number of Students Unable to Eat Lunch - Solution & Explanation

EasyArrayStackQueueSimulation21 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.

The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:

  • If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
  • Otherwise, they will leave it and go to the queue's end.

This continues until none of the queue students want to take the top sandwich and are thus unable to eat.

You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the i​​​​​​th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

 

Example 1:

Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
Output: 0 
Explanation:
- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
Hence all students are able to eat.

Example 2:

Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
Output: 3

 

Constraints:

  • 1 <= students.length, sandwiches.length <= 100
  • students.length == sandwiches.length
  • sandwiches[i] is 0 or 1.
  • students[i] is 0 or 1.

Approach Overview

Problem Overview: Students stand in a queue and sandwiches are stacked. Each student prefers either circular (0) or square (1) sandwiches. If the front student does not want the top sandwich, they move to the end of the queue. The process stops when nobody in the queue wants the sandwich on top. Return the number of students who cannot eat.

Approach 1: Simulate the Process Using a Queue (Time: O(n^2), Space: O(n))

This approach models the situation exactly as described. Put all students into a queue and process sandwiches from the stack. If the front student prefers the current sandwich type, they take it and both are removed. Otherwise, move that student to the end of the queue. Track how many consecutive students have been rotated without anyone taking the sandwich. If this count reaches the current queue size, no remaining student wants the sandwich on top, so the process stops. This method directly uses queue operations like pop and push and mirrors the real process step by step. It’s easy to reason about and works well for understanding the simulation aspect of the problem using a queue or simulation pattern.

Approach 2: Count Preferences and Match with Sandwiches (Time: O(n), Space: O(1))

The key observation: the relative order of students does not matter once you know how many students prefer each sandwich type. Count how many students want circular (0) and square (1) sandwiches. Then iterate through the sandwich stack from top to bottom. If the sandwich is type 0 and there are students who want 0, decrement the count and continue. If no students want that type anymore, the remaining students cannot eat because the sandwich on top will never be taken. The same logic applies for type 1. This removes the need for an explicit queue and reduces the simulation to simple counting. The algorithm only scans the arrays once, making it an efficient array-based solution.

Recommended for interviews: The counting approach is the expected optimal solution because it runs in O(n) time with constant space and relies on a key insight about preference counts. Showing the queue simulation first demonstrates you understand the problem mechanics. Then optimizing it using counts shows strong problem-solving and pattern recognition.

Approach 1: Simulate the Process Using a Queue

In this approach, we simulate the exact process of distributing sandwiches using a queue-like structure for the students. We repeatedly check if the student at the front of the queue can take the sandwich at the top of the stack. If not, the student is moved to the back of the queue. This continues until we complete a full cycle without any students taking a sandwich, indicating they can't eat.

This C code defines a function that uses a circular iteration over the student preferences to check each against the current sandwich on the stack. We use two indices: one for students and one for sandwiches, plus a cycle counter to detect when all options are exhausted without making progress. The cycle count control helps to stop the loop when no further matches are possible, making the method more efficient.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2) in the worst scenario because each student may be checked multiple times.
Space Complexity: O(1) since it only uses a fixed amount of extra space.

Try this approach in the editor β†’

Approach 2: Count Preferences and Match with Sandwiches

This approach uses counting to determine if students can satisfy their preferences before iterating over sandwiches. By counting the number of students preferring each type, we can efficiently determine how many students will be unable to eat if sandwiches of their preference run out before they can eat.

The C implementation counts the preferences for circular and square sandwiches. It iterates over the sandwiches and decreases the count of the suitable preference whenever a sandwich is taken. If no more students prefer the top sandwich, it returns the remaining count.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) because it processes each list one time.
Space Complexity: O(1) since it only uses fixed additional space for counting.

Try this approach in the editor β†’

Approach 3: Counting

We observe that the positions of the students can be adjusted, but the positions of the sandwiches cannot be adjusted. That is to say, if the sandwich in front is not taken, then all the sandwiches behind cannot be taken.

Therefore, we first use a counter cnt to count the types of sandwiches that students like and their corresponding quantities.

Then we traverse the sandwiches. If we cannot find a student who likes this sandwich in cnt, it means that the sandwiches behind cannot be taken, and we return the current number of remaining students.

If the traversal is over, it means that all students have sandwiches to eat, and we return 0.

The time complexity is O(n), where n is the number of sandwiches. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
Simulate the Process Using a Queue

Time Complexity: O(n^2) in the worst scenario because each student may be checked multiple times.
Space Complexity: O(1) since it only uses a fixed amount of extra space.

Count Preferences and Match with Sandwiches

Time Complexity: O(n) because it processes each list one time.
Space Complexity: O(1) since it only uses fixed additional space for counting.

Countingβ€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simulate the Process Using a QueueO(n^2)O(n)When you want a direct simulation of the queue behavior or when learning queue-based problem modeling
Count Preferences and Match with SandwichesO(n)O(1)Best for interviews and large inputs where an optimal linear scan is preferred

Video Solution

Number of Students Unable to Eat Lunch - Leetcode 1700 - Python β€’ NeetCodeIO β€’ 30,691 views views

Watch 9 more video solutions β†’

Frequently Asked Questions

Is Number of Students Unable to Eat Lunch easy or hard?
Number of Students Unable to Eat Lunch is classified as an Easy problem on LeetCode with a high acceptance rate. The challenge mainly tests understanding of queue simulation and recognizing the optimization that converts the process into simple counting.
Number of Students Unable to Eat Lunch Python/Java solution
Python and Java implementations typically follow either the queue simulation using collections like deque/Queue or the optimal counting approach using simple integer counters. The counting method is shorter, runs in O(n) time, and uses constant extra space.
How to solve Number of Students Unable to Eat Lunch in O(n)?
Count how many students prefer sandwich type 0 and type 1. Iterate through the sandwiches stack from top to bottom. If the sandwich type still has students who want it, decrement the corresponding counter. If not, stop immediately because the remaining students cannot eat. The number of remaining students is the answer.
What is the best approach for Number of Students Unable to Eat Lunch?
The best approach counts how many students prefer each sandwich type (0 or 1) and then scans the sandwich stack. If the current sandwich type has no students left who prefer it, the remaining students cannot eat. This method runs in O(n) time and O(1) space and avoids unnecessary queue rotations.
Is Number of Students Unable to Eat Lunch asked at Google/Amazon/Meta?
This problem represents a common queue and simulation pattern frequently used in technical interviews. Variations of queue processing and preference matching have appeared in interviews at companies like Amazon and Google, especially for entry-level and mid-level roles.
What data structure is used in Number of Students Unable to Eat Lunch?
The direct simulation uses a queue to represent the line of students and processes sandwiches from a stack-like structure. The optimized solution reduces the problem to counting values in an array, eliminating the need for explicit queue operations.
What is the time complexity of Number of Students Unable to Eat Lunch?
The optimal counting solution runs in O(n) time because you count student preferences once and iterate through the sandwiches once. The queue simulation approach can degrade to O(n^2) in the worst case due to repeated rotations of students in the queue.

Ready to solve this problem?

Practice Number of Students Unable to Eat Lunch with our built-in code editor and test cases.

Practice on FleetCode