Count Students Left When Lunch Service Stops

Quick Overview

Count Students Left When Lunch Service Stops. Students queue with preferences 0 or 1. sandwiches lists a stack from top to bottom, using the same two types. A front student takes a matching top sandwich and leaves; otherwise the student moves to the back.

Count Students Left When Lunch Service Stops

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Students queue with preferences 0 or 1. `sandwiches` lists a stack from top to bottom, using the same two types. A front student takes a matching top sandwich and leaves; otherwise the student moves to the back. Stop when no remaining student wants the top sandwich. Return how many students cannot eat. ```cpp class Solution { public: int countStudents(vector<int>& students, vector<int>& sandwiches); }; ``` ### Examples **Example 1:** ```text students = [0,1,0], sandwiches = [1,0,0] Output: 0 Explanation: Rotation brings the type-1 student forward, then both type-0 students eat. ``` **Example 2:** ```text students = [1,0,1], sandwiches = [0,0,1] Output: 2 Explanation: The only type-0 student takes the first sandwich; the next type-0 sandwich blocks both remaining students. ``` ### Constraints - The arrays have equal length in `[1,100]`. - Every student preference and sandwich type is either 0 or 1. - Index zero is the queue front or the stack top, respectively. ### Clarifying Questions Sandwich order cannot change. Queue rotation can reach any remaining student but cannot skip an unwanted top sandwich. ```hint Inspect the structure When does the stated queue process stop, and can a later sandwich be considered before the top one? ``` ### Reasoning checkpoints - Show that each served sandwich follows the fixed stack order and the queue rules. - Justify the stopping condition and the exact remaining-student count. ### Optional extensions How would the counting method extend to more than two sandwich types?

Overview: Count Students Left When Lunch Service Stops. Students queue with preferences 0 or 1. sandwiches lists a stack from top to bottom, using the same two types. A front student takes a matching top sandwich and leaves; otherwise the student moves to the back.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Students queue with preferences 0 or 1. sandwiches lists a stack from top to bottom, using the same two types. A front student takes a matching top sandwich and leaves; otherwise the student moves to the back. Stop when no remaining student wants the top sandwich. Return how many students cannot eat.

class Solution {
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches);
};

Examples

Example 1:

students = [0,1,0], sandwiches = [1,0,0]
Output: 0
Explanation: Rotation brings the type-1 student forward, then both type-0 students eat.

Example 2:

students = [1,0,1], sandwiches = [0,0,1]
Output: 2
Explanation: The only type-0 student takes the first sandwich; the next type-0 sandwich blocks both remaining students.

Constraints

  • The arrays have equal length in [1,100] .
  • Every student preference and sandwich type is either 0 or 1.
  • Index zero is the queue front or the stack top, respectively.

Clarifying Questions Guidance

Sandwich order cannot change. Queue rotation can reach any remaining student but cannot skip an unwanted top sandwich.

Reasoning checkpoints

  • Show that each served sandwich follows the fixed stack order and the queue rules.
  • Justify the stopping condition and the exact remaining-student count.

Optional extensions

How would the counting method extend to more than two sandwich types?