Challenge - 5 Problems
Deque Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Deque operations sequence
What is the printed state of the deque after performing these operations in order?
Operations:
1. Add 10 to the front
2. Add 20 to the back
3. Add 30 to the front
4. Remove from the back
5. Add 40 to the back
Operations:
1. Add 10 to the front
2. Add 20 to the back
3. Add 30 to the front
4. Remove from the back
5. Add 40 to the back
DSA Go
package main import ( "container/list" "fmt" ) func main() { deque := list.New() deque.PushFront(10) // Step 1 deque.PushBack(20) // Step 2 deque.PushFront(30) // Step 3 deque.Remove(deque.Back()) // Step 4 deque.PushBack(40) // Step 5 for e := deque.Front(); e != nil; e = e.Next() { fmt.Print(e.Value, " -> ") } fmt.Print("null") }
Attempts:
2 left
💡 Hint
Remember that PushFront adds to the front and PushBack adds to the back. Removing from the back removes the last element.
✗ Incorrect
Step 1 adds 10 front: 10
Step 2 adds 20 back: 10 -> 20
Step 3 adds 30 front: 30 -> 10 -> 20
Step 4 removes back (20): 30 -> 10
Step 5 adds 40 back: 30 -> 10 -> 40
🧠 Conceptual
intermediateDeque behavior on empty removal
What happens if you try to remove an element from the front of an empty deque implemented using Go's container/list?
Attempts:
2 left
💡 Hint
Check the behavior of list.Remove when passed nil.
✗ Incorrect
Calling deque.Remove(deque.Front()) on an empty deque: Front() returns nil, and Remove(nil) panics with a nil pointer dereference when accessing e.list inside Remove.
🔧 Debug
advancedIdentify the bug in this deque implementation snippet
What is the bug in this Go code snippet that tries to remove an element from the front of a deque and print it?
Code:
deque := list.New()
deque.PushBack(5)
front := deque.Front()
deque.Remove(front)
fmt.Println(front.Value)
Code:
deque := list.New()
deque.PushBack(5)
front := deque.Front()
deque.Remove(front)
fmt.Println(front.Value)
DSA Go
package main import ( "container/list" "fmt" ) func main() { deque := list.New() deque.PushBack(5) front := deque.Front() deque.Remove(front) fmt.Println(front.Value) }
Attempts:
2 left
💡 Hint
Think about what happens to the element after Remove is called.
✗ Incorrect
front.Value prints 5 correctly; no bug. After Remove, the Element still holds its Value field, which can be safely accessed.
❓ Predict Output
advancedOutput after mixed deque operations
What is the output of the following Go program that uses a deque?
Code:
package main
import ("container/list"; "fmt")
func main() {
d := list.New()
d.PushBack(1)
d.PushBack(2)
d.PushFront(3)
d.Remove(d.Back())
d.PushFront(4)
for e := d.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " -> ")
}
fmt.Print("null")
}
Code:
package main
import ("container/list"; "fmt")
func main() {
d := list.New()
d.PushBack(1)
d.PushBack(2)
d.PushFront(3)
d.Remove(d.Back())
d.PushFront(4)
for e := d.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " -> ")
}
fmt.Print("null")
}
DSA Go
package main import ( "container/list" "fmt" ) func main() { d := list.New() d.PushBack(1) d.PushBack(2) d.PushFront(3) d.Remove(d.Back()) d.PushFront(4) for e := d.Front(); e != nil; e = e.Next() { fmt.Print(e.Value, " -> ") } fmt.Print("null") }
Attempts:
2 left
💡 Hint
Track each operation carefully, especially removals and additions at front and back.
✗ Incorrect
Initial: empty
PushBack(1): 1
PushBack(2): 1 -> 2
PushFront(3): 3 -> 1 -> 2
Remove Back (2): 3 -> 1
PushFront(4): 4 -> 3 -> 1
🚀 Application
expertDeque usage for sliding window maximum problem
Given a sliding window size k=3 and input array [2, 1, 3, 4, 6, 3, 8, 9, 10, 12, 56], which of the following outputs correctly represents the maximum values in each sliding window using a deque-based approach?
Attempts:
2 left
💡 Hint
The sliding window maximum moves one step at a time, always picking the max in the current window.
✗ Incorrect
The sliding windows of size 3 are:
[2,1,3] max=3
[1,3,4] max=4
[3,4,6] max=6
[4,6,3] max=6
[6,3,8] max=8
[3,8,9] max=9
[8,9,10] max=10
[9,10,12] max=12
[10,12,56] max=56
[2,1,3] max=3
[1,3,4] max=4
[3,4,6] max=6
[4,6,3] max=6
[6,3,8] max=8
[3,8,9] max=9
[8,9,10] max=10
[9,10,12] max=12
[10,12,56] max=56
