Implement a fixed-size queue data structure that stores integers. The queue should have a maximum size and support enqueue and dequeue operations. Return -1 when trying to dequeue from an empty queue or enqueue to a full queue.
Examples:
Input: operations = [
'enqueue 1', // returns 1
'enqueue 2', // returns 2
'enqueue 3', // returns -1 (queue is full, size=2)
'dequeue', // returns 1
'enqueue 3', // returns 3
'dequeue' // returns 2
]
maxSize = 2
Output: [1, 2, -1, 1, 3, 2]
Explanation: Queue operations with max size 2, showing return values for each operation
Input: operations = [
'dequeue', // returns -1 (empty queue)
'enqueue 1', // returns 1
'dequeue' // returns 1
]
maxSize = 1
Output: [-1, 1, 1]
Explanation: Queue operations with max size 1, starting with dequeue on empty queue
Method used to process your code
public static List<Integer> processQueueOperations(List<String> operations, Integer maxSize) {
FixedSizeQueue queue = new FixedSizeQueue(maxSize);
List<Integer> results = new List<Integer>();
for (String operation : operations) {
if (operation.startsWith('enqueue')) {
Integer item = Integer.valueOf(operation.split(' ')[1]);
results.add(queue.enqueue(item));
} else if (operation == 'dequeue') {
results.add(queue.dequeue());
}
}
return results;
}
Apex Code Editor
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Note
🏆 MVP Contest 2: The #1 ranked player on our global leaderboard by February 28th will receive a free Salesforce $200 certification voucher! Contest runs Feb 15-28. Track your position and compete for the prize! Points are only calculated for questions created and completed during the contest period.