Bird
Raised Fist0
DSA Goprogramming~20 mins

Peek Front Element of Queue in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue Peek Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after peeking the front element of the queue?
Consider the following Go code that implements a queue and peeks the front element. What will be printed?
DSA Go
package main
import "fmt"

type Queue struct {
    items []int
}

func (q *Queue) Enqueue(val int) {
    q.items = append(q.items, val)
}

func (q *Queue) Peek() int {
    if len(q.items) == 0 {
        return -1
    }
    return q.items[0]
}

func main() {
    q := Queue{}
    q.Enqueue(10)
    q.Enqueue(20)
    q.Enqueue(30)
    fmt.Println(q.Peek())
}
A-1
B20
C30
D10
Attempts:
2 left
💡 Hint
Peek returns the first element without removing it.
❓ Predict Output
intermediate
2:00remaining
What happens when peeking an empty queue?
Given the following Go code, what will be the output when peeking an empty queue?
DSA Go
package main
import "fmt"

type Queue struct {
    items []int
}

func (q *Queue) Peek() int {
    if len(q.items) == 0 {
        return -1
    }
    return q.items[0]
}

func main() {
    q := Queue{}
    fmt.Println(q.Peek())
}
Apanic: runtime error
B0
C-1
Dnil
Attempts:
2 left
💡 Hint
Peek returns -1 if the queue is empty.
🔧 Debug
advanced
2:00remaining
Identify the error in the Peek method implementation
What error will occur when running this Go code that tries to peek the front element of the queue?
DSA Go
package main
import "fmt"

type Queue struct {
    items []int
}

func (q *Queue) Peek() int {
    return q.items[0]
}

func main() {
    q := Queue{}
    fmt.Println(q.Peek())
}
AIndex out of range runtime panic
BReturns 0
CCompilation error: missing return statement
DReturns -1
Attempts:
2 left
💡 Hint
Peek does not check if the queue is empty before accessing the first element.
🧠 Conceptual
advanced
2:00remaining
What is the time complexity of peeking the front element in a queue implemented with a slice?
Consider a queue implemented using a slice in Go. What is the time complexity of the Peek operation?
AO(1) - constant time
BO(n^2) - quadratic time
CO(log n) - logarithmic time
DO(n) - linear time
Attempts:
2 left
💡 Hint
Peek just returns the first element without iteration.
🚀 Application
expert
3:00remaining
After these operations, what is the front element of the queue?
Given the following Go code, what will be printed after all operations?
DSA Go
package main
import "fmt"

type Queue struct {
    items []int
}

func (q *Queue) Enqueue(val int) {
    q.items = append(q.items, val)
}

func (q *Queue) Dequeue() int {
    if len(q.items) == 0 {
        return -1
    }
    val := q.items[0]
    q.items = q.items[1:]
    return val
}

func (q *Queue) Peek() int {
    if len(q.items) == 0 {
        return -1
    }
    return q.items[0]
}

func main() {
    q := Queue{}
    q.Enqueue(5)
    q.Enqueue(10)
    q.Enqueue(15)
    q.Dequeue()
    q.Enqueue(20)
    fmt.Println(q.Peek())
}
A15
B10
C5
D20
Attempts:
2 left
💡 Hint
Dequeue removes the first element, Peek shows the new front.

Practice

(1/5)
1. What does the Peek operation do in a queue?
easy
A. Returns the front element without removing it
B. Removes the front element from the queue
C. Adds an element to the back of the queue
D. Checks if the queue is empty

Solution

  1. Step 1: Understand the queue operations

    A queue supports adding elements at the back and removing from the front.
  2. Step 2: Define Peek operation

    Peek returns the front element without removing it, unlike dequeue which removes it.
  3. Final Answer:

    Returns the front element without removing it -> Option A
  4. Quick Check:

    Peek = front element without removal [OK]
Hint: Peek shows front without removal, unlike dequeue [OK]
Common Mistakes:
  • Confusing peek with dequeue
  • Thinking peek adds elements
  • Assuming peek checks emptiness
2. Which of the following is the correct Go syntax to peek the front element of a queue implemented as a slice named queue?
easy
A. front := queue[len(queue)-1]
B. front := queue[len(queue)]
C. front := queue[0]
D. front := queue.pop()

Solution

  1. Step 1: Understand Go slice indexing

    Go slices are zero-indexed, so the first element is at index 0.
  2. Step 2: Identify front element access

    The front element of a queue is the first element, so queue[0] gives it.
  3. Final Answer:

    front := queue[0] -> Option C
  4. Quick Check:

    First element index = 0 [OK]
Hint: Front element is always at index 0 in slice queue [OK]
Common Mistakes:
  • Using len(queue) which is out of range
  • Using last element instead of first
  • Using pop() which is not a Go slice method
3. What will be the output of the following Go code snippet?
queue := []int{10, 20, 30}
front := queue[0]
fmt.Println(front)
medium
A. runtime error
B. 10
C. 30
D. 20

Solution

  1. Step 1: Initialize queue slice

    The queue slice contains elements [10, 20, 30].
  2. Step 2: Access front element

    front := queue[0] assigns 10 to front.
  3. Step 3: Print front

    fmt.Println(front) prints 10.
  4. Final Answer:

    10 -> Option B
  5. Quick Check:

    queue[0] = 10 [OK]
Hint: queue[0] gives first element value [OK]
Common Mistakes:
  • Printing queue[1] instead of queue[0]
  • Expecting last element output
  • Thinking it causes runtime error
4. Identify the error in this Go code that tries to peek the front element of a queue:
var queue []int
front := queue[0]
fmt.Println(front)
medium
A. No error, prints 0
B. Prints nil
C. Compile-time syntax error
D. Runtime panic: index out of range

Solution

  1. Step 1: Check queue initialization

    queue is declared but not initialized, so length is 0.
  2. Step 2: Accessing queue[0]

    Accessing queue[0] causes runtime panic because slice is empty.
  3. Final Answer:

    Runtime panic: index out of range -> Option D
  4. Quick Check:

    Empty slice access causes panic [OK]
Hint: Empty slice access causes runtime panic [OK]
Common Mistakes:
  • Assuming default zero value is returned
  • Expecting compile-time error
  • Thinking it prints nil
5. Given a queue implemented as a slice queue := []int{5, 10, 15}, write the correct Go code snippet to safely peek the front element and handle the empty queue case by printing "Empty" if no elements exist.
hard
A. if len(queue) > 0 { fmt.Println(queue[0]) } else { fmt.Println("Empty") }
B. fmt.Println(queue[0])
C. if queue != nil { fmt.Println(queue[0]) } else { fmt.Println("Empty") }
D. if len(queue) == 0 { fmt.Println(queue[0]) } else { fmt.Println("Empty") }

Solution

  1. Step 1: Check queue length before access

    Use len(queue) > 0 to ensure queue has elements.
  2. Step 2: Print front or "Empty"

    If queue has elements, print queue[0]; else print "Empty".
  3. Final Answer:

    if len(queue) > 0 { fmt.Println(queue[0]) } else { fmt.Println("Empty") } -> Option A
  4. Quick Check:

    Check length before access to avoid panic [OK]
Hint: Always check length before peeking front [OK]
Common Mistakes:
  • Not checking length before access
  • Checking nil instead of length
  • Reversing if-else logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard