Challenge - 5 Problems
Dequeue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dequeue after multiple insertions and deletions
What is the printed state of the dequeue after the following operations?
DSA Java
import java.util.Deque; import java.util.LinkedList; public class Main { public static void main(String[] args) { Deque<Integer> dq = new LinkedList<>(); dq.addFirst(10); dq.addLast(20); dq.addFirst(5); dq.removeLast(); dq.addLast(15); System.out.println(dq); } }
Attempts:
2 left
💡 Hint
Remember addFirst adds to front, addLast adds to end, removeLast removes from end.
✗ Incorrect
Initially, dq is empty. addFirst(10) -> [10]. addLast(20) -> [10, 20]. addFirst(5) -> [5, 10, 20]. removeLast() removes 20 -> [5, 10]. addLast(15) -> [5, 10, 15].
❓ Predict Output
intermediateResult of peek operations on Dequeue
What will be the output of the following code snippet?
DSA Java
import java.util.Deque; import java.util.LinkedList; public class Main { public static void main(String[] args) { Deque<String> dq = new LinkedList<>(); dq.addLast("apple"); dq.addLast("banana"); dq.addFirst("cherry"); System.out.println(dq.peekFirst()); System.out.println(dq.peekLast()); } }
Attempts:
2 left
💡 Hint
peekFirst returns front element, peekLast returns last element.
✗ Incorrect
After addLast("apple") and addLast("banana"), dq is [apple, banana]. addFirst("cherry") makes it [cherry, apple, banana]. peekFirst() returns "cherry", peekLast() returns "banana".
🔧 Debug
advancedIdentify the error in Dequeue removal code
What error will the following code produce when executed?
DSA Java
import java.util.Deque; import java.util.LinkedList; public class Main { public static void main(String[] args) { Deque<Integer> dq = new LinkedList<>(); dq.addLast(1); dq.addLast(2); dq.removeFirst(); dq.removeFirst(); dq.removeFirst(); } }
Attempts:
2 left
💡 Hint
What happens if you remove from an empty dequeue using removeFirst()?
✗ Incorrect
After removing twice, the dequeue is empty. The third removeFirst() call throws NoSuchElementException because there is no element to remove.
❓ Predict Output
advancedOutput after mixed add and remove operations on Dequeue
What is the output of the following code?
DSA Java
import java.util.Deque; import java.util.LinkedList; public class Main { public static void main(String[] args) { Deque<Character> dq = new LinkedList<>(); dq.addLast('A'); dq.addFirst('B'); dq.addLast('C'); dq.removeFirst(); dq.addFirst('D'); dq.removeLast(); System.out.println(dq); } }
Attempts:
2 left
💡 Hint
Track each operation step by step carefully.
✗ Incorrect
Start: []. addLast('A') -> [A]. addFirst('B') -> [B, A]. addLast('C') -> [B, A, C]. removeFirst() removes B -> [A, C]. addFirst('D') -> [D, A, C]. removeLast() removes C -> [D, A].
🧠 Conceptual
expertDequeue behavior under concurrent modifications
In a multi-threaded Java program using a LinkedList as a Dequeue, what issue can arise if multiple threads modify the dequeue without synchronization?
Attempts:
2 left
💡 Hint
Think about thread safety of LinkedList and what happens when multiple threads modify it.
✗ Incorrect
LinkedList is not thread-safe. Concurrent modifications without synchronization can cause data inconsistency and throw ConcurrentModificationException during iteration.
