📖 Scenario: You are building a simple task manager that can add and remove tasks from both the front and back of the list. This helps you quickly manage urgent tasks or finish tasks in order.
🎯 Goal: Build a double ended queue (deque) using a slice in Go. You will add tasks to the front and back, remove tasks from both ends, and finally print the current list of tasks.
📋 What You'll Learn
Create a slice of strings called tasks with initial tasks
Create a variable newTask to hold a task to add
Add newTask to the front of tasks
Add newTask to the back of tasks
Remove one task from the front and one from the back of tasks
Print the final state of tasks showing all tasks in order
💡 Why This Matters
🌍 Real World
Double ended queues are useful in task scheduling, undo-redo operations, and managing buffers where you need to add or remove items from both ends efficiently.
💼 Career
Understanding deque operations helps in software development roles involving data processing, real-time systems, and UI state management.
Progress0 / 4 steps
1
Create initial tasks slice
Create a slice of strings called tasks with these exact tasks in order: "email client", "write report", "call supplier"
DSA Go
Hint
Use a slice literal to create tasks with the exact strings in order.
2
Add new task variable
Create a string variable called newTask and set it to "schedule meeting"
DSA Go
Hint
Declare newTask as a string and assign the exact value.
3
Add and remove tasks from deque
Add newTask to the front of tasks, then add newTask to the back of tasks. Next, remove one task from the front and one from the back of tasks.
DSA Go
Hint
Use append to add to front and back. Use slicing to remove from front and back.
4
Print final tasks list
Print the tasks slice to show the final list of tasks in order.
DSA Go
Hint
Use fmt.Println(tasks) to print the slice.
Practice
(1/5)
1. What is a Double Ended Queue (Deque)?
easy
A. A data structure where elements can be added or removed from both ends
B. A queue where elements can only be added at the rear
C. A stack where elements can only be added or removed from the top
D. A list that allows random access to elements
Solution
Step 1: Understand the definition of Deque
A Deque allows insertion and deletion from both front and rear ends, unlike a queue or stack.
Step 2: Compare with other data structures
Queue allows insertion at rear and deletion at front; stack allows insertion and deletion only at one end.
Final Answer:
A data structure where elements can be added or removed from both ends -> Option A
Quick Check:
Deque = double-ended queue [OK]
Hint: Deque means double-ended queue, both ends usable [OK]
Common Mistakes:
Confusing deque with stack or queue
Thinking deque allows random access
Assuming insertion only at one end
2. Which of the following is the correct way to declare a deque using Go's container/list package?
easy
A. var d []int
B. d := list.New()
C. d := make(deque, 0)
D. var d map[int]int
Solution
Step 1: Recall Go's deque implementation
Go does not have a built-in deque type but container/list can be used as a double-ended queue.
Step 2: Check syntax for creating a new list
list.New() returns a pointer to a new doubly linked list, suitable for deque operations.
Final Answer:
d := list.New() -> Option B
Quick Check:
Use list.New() for deque in Go [OK]
Hint: Use list.New() to create a deque in Go [OK]
Common Mistakes:
Using slice instead of list for deque
Trying to make a deque type directly
Confusing map with deque
3. What will be the output of the following Go code using container/list as a deque?
package main
import (
"container/list"
"fmt"
)
func main() {
d := list.New()
d.PushBack(10)
d.PushFront(20)
d.PushBack(30)
for e := d.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value, " ")
}
}
medium
A. 30 10 20
B. 10 20 30
C. 20 10 30
D. 20 30 10
Solution
Step 1: Trace the Push operations on deque
PushBack(10) adds 10 at rear; PushFront(20) adds 20 at front; PushBack(30) adds 30 at rear.
Step 2: Traverse from front to back
Elements order: 20 (front), 10 (middle), 30 (rear).
4. Identify the error in this Go code snippet using container/list as a deque:
package main
import (
"container/list"
"fmt"
)
func main() {
d := list.New()
d.PushFront(1)
d.Remove(d.Back())
fmt.Println(d.Back().Value)
}
medium
A. Using PushFront instead of PushBack
B. Not initializing the list with make()
C. Printing d.Back().Value without converting to string
D. Calling Remove on d.Back() before checking if d.Back() is nil
Solution
Step 1: Understand Remove and Back usage
d.Remove(d.Back()) removes the last element; after removal, d.Back() may be nil if list is empty.
Step 2: Check the print statement
fmt.Println(d.Back().Value) accesses Value of d.Back() without checking if d.Back() is nil, causing runtime panic if nil.
Final Answer:
Calling Remove on d.Back() before checking if d.Back() is nil -> Option D
Quick Check:
Check nil before accessing Back() after Remove [OK]
Hint: Check nil after Remove before accessing Back() [OK]
Common Mistakes:
Ignoring nil check after removal
Confusing PushFront and PushBack
Thinking fmt.Println needs string conversion
5. You want to implement a sliding window maximum using a deque in Go. Which deque operation sequence correctly maintains the window elements in decreasing order from front to rear?
// Assume d is a deque of indices
for i := 0; i < len(nums); i++ {
for d.Len() > 0 && nums[d.Back().Value.(int)] <= nums[i] {
d.Remove(d.Back())
}
d.PushBack(i)
if d.Front().Value.(int) <= i - k {
d.Remove(d.Front())
}
}
hard
A. Remove from back while current element is greater or equal, then add current index at back
B. Remove from front while current element is smaller, then add current index at front
C. Add current index at front without removals
D. Remove from back while current element is smaller, then add current index at front
Solution
Step 1: Understand sliding window max logic
We remove smaller or equal elements from back to keep deque decreasing, then add current index at back.
Step 2: Remove indices out of window from front
If front index is out of current window (i - k), remove it from front to maintain window size.
Final Answer:
Remove from back while current element is greater or equal, then add current index at back -> Option A
Quick Check:
Sliding window max uses back removals and back insertions [OK]
Hint: Remove smaller from back, add new at back for max window [OK]
Common Mistakes:
Removing from front instead of back for smaller elements
Adding new elements at front incorrectly
Not removing out-of-window indices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.