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;
}
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
Contest Alert
The #1 ranked player on our #CodeEveryDay leaderboard by April 30th will receive a free Salesforce PD1 certification voucher!
Contest runs April 18-30. Additional prizes available for other top leaderboard positions. Track your position and compete for prizes!
Points are only calculated for questions created and completed during the contest period. If you previously won, you are not eligible to win the PD1 certification again.