Frontend School

Recent Ticket Tracker

DSA
easy

Recent Ticket Tracker

Easy
Queue
Data Structures
Arrays

Overview

At Freshworks, efficiently managing support tickets is crucial for customer satisfaction. Our support systems often need to keep track of the most recent customer interactions to provide quick context to agents. This problem simulates a core component of such a system: a tracker that maintains a fixed number of the latest support ticket IDs.

What you need to do / Task

Your task is to implement a function getRecentTickets that simulates a fixed-size queue for tracking support ticket IDs. Given a stream of incoming ticket IDs and a maximum capacity for the tracker, your function should return the final state of the tracker, containing only the capacity most recently added tickets.

When a new ticket ID arrives:

  1. If the tracker is not yet full (i.e., it holds fewer than capacity tickets), the new ticket ID is simply added to the end of the tracker.
  2. If the tracker is already full (i.e., it holds capacity tickets), the oldest ticket ID (at the front of the tracker) must be removed to make space, and then the new ticket ID is added to the end.

After all ticket IDs from the ticketStream have been processed, return the contents of the tracker in the order they were added (from oldest to newest within the tracker).

Input & output

  • Input:

    • ticketStream: An array of numbers (number[]) representing the sequence of support ticket IDs arriving over time.
    • capacity: A number (number) representing the maximum number of tickets the tracker can hold at any given time.
  • Output:

    • An array of numbers (number[]) representing the final state of the tracker, containing the capacity most recent ticket IDs, ordered from oldest to newest.

Clarifications

  • Empty ticketStream: If ticketStream is empty, the function should return an empty array [].
  • capacity vs. ticketStream length: If capacity is greater than the total number of tickets in ticketStream, the tracker will simply contain all tickets from ticketStream.
  • Duplicate Ticket IDs: Ticket IDs may not be unique. Treat each incoming ID as a distinct entry for tracking purposes.

Examples

1

Example 1

Input:

ticketStream = [101, 102, 103, 104, 105], capacity = 3

Output:

[103,104,105]

Note:

  1. [101]
  2. [101, 102]
  3. [101, 102, 103] (Queue full)
  4. [102, 103, 104] (101 removed, 104 added)
  5. [103, 104, 105] (102 removed, 105 added) Final state: [103, 104, 105]
2

Example 2

Input:

ticketStream = [201, 202], capacity = 5

Output:

[201,202]

Note:

Since capacity (5) is greater than ticketStream length (2), all tickets are kept. Final state: [201, 202].

3

Example 3

Input:

ticketStream = [], capacity = 2

Output:

[]

Note:

An empty ticket stream results in an empty tracker.

4

Example 4

Input:

ticketStream = [301, 302, 303, 304], capacity = 1

Output:

[304]

Note:

With capacity 1, only the very last ticket processed remains. 301 -> [301]; 302 -> [302]; 303 -> [303]; 304 -> [304]. Final state: [304].

Constraints

Constraints & assumptions

  • 0 <= ticketStream.length <= 10^4
  • 1 <= ticketStream[i] <= 10^9 (Ticket IDs are positive integers)
  • 1 <= capacity <= 10^4

What interviewers evaluate

  • Correctness: Does the solution accurately simulate the fixed-size queue behavior, handling additions and removals correctly?
  • Edge Cases: Is the solution robust to edge cases such as empty input, capacity being 1, or capacity being larger than the stream length?
  • Efficiency: Does the solution process the ticket stream with optimal time and space complexity?
  • Code Quality: Is the code clean, readable, and well-structured?

Loading editor...