Recent Ticket Tracker
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.
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:
capacity tickets), the new ticket ID is simply added to the end of the tracker.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:
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:
number[]) representing the final state of the tracker, containing the capacity most recent ticket IDs, ordered from oldest to newest.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.Examples
Example 1
Input:
ticketStream = [101, 102, 103, 104, 105], capacity = 3
Output:
[103,104,105]
Note:
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].
Example 3
Input:
ticketStream = [], capacity = 2
Output:
[]
Note:
An empty ticket stream results in an empty tracker.
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
0 <= ticketStream.length <= 10^41 <= ticketStream[i] <= 10^9 (Ticket IDs are positive integers)1 <= capacity <= 10^4capacity being 1, or capacity being larger than the stream length?Loading editor...