easyArraysQueue 1 views

Serve the First K Customers in Line

Practice using a queue (Queue/Deque) to process items in first-in, first-out order.

Customers are waiting in a line, given as an array line where line[0] is at the front. Simulate a queue: serve customers from the front, one at a time, until you've served k of them or the line is empty. Return the customer IDs in the order they were served.

This is a basics exercise for queues (Queue/ArrayDeque in Java, Queue<T> in C#, collections.deque in Python, an array used with shift/push in JS/TS) — first-in, first-out (FIFO) processing.

Example 1

Input: line = [101,102,103,104], k = 2

Output: [101,102]

Explanation: The first 2 customers in line are served in order.

Example 2

Input: line = [5], k = 3

Output: [5]

Explanation: Only 1 customer exists, so only 1 is served.

Example 3

Input: line = [7,8,9], k = 0

Output: []

Constraints

  • 0 <= line.length <= 1000
  • 0 <= k <= 1000

Follow-up

How would you implement a queue using two stacks instead of a built-in Queue type?

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.