Step 2: Identify exception on removing from empty Deque
removeFirst() on empty Deque throws NoSuchElementException
Final Answer:
Throws NoSuchElementException on last removeFirst() -> Option D
Quick Check:
Removing from empty Deque throws NoSuchElementException [OK]
Hint: Removing from empty Deque throws NoSuchElementException [OK]
Common Mistakes:
Assuming removeFirst returns null instead of exception
Confusing method names causing compile error
Thinking removeLast throws NullPointerException
5. You want to implement a task scheduler that processes tasks in the order they arrive but sometimes needs to prioritize urgent tasks by adding them to the front. Which Deque operation combination is best suited for this scenario?
hard
A. Use addLast() for normal tasks and addFirst() for urgent tasks
B. Use addFirst() for normal tasks and addLast() for urgent tasks
C. Use only addLast() for all tasks
D. Use only addFirst() for all tasks
Solution
Step 1: Understand task order requirements
Normal tasks should be processed in arrival order (queue behavior). Urgent tasks should be processed before normal tasks (front insertion).
Step 2: Match Deque operations to requirements
addLast() adds normal tasks at rear (end), preserving arrival order. addFirst() adds urgent tasks at front, prioritizing them.
Final Answer:
Use addLast() for normal tasks and addFirst() for urgent tasks -> Option A
Quick Check:
Normal at rear, urgent at front [OK]
Hint: Normal tasks rear, urgent tasks front with addLast/addFirst [OK]
Common Mistakes:
Reversing addFirst and addLast roles
Using only one end for all tasks losing priority
Confusing queue and stack behavior
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.