Bird
Raised Fist0
DSA Goprogramming~20 mins

Push Operation on Stack in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Push 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 pushing elements on a stack?
Consider a stack implemented using a slice in Go. What will be the printed stack after pushing 10, then 20, then 30?
DSA Go
package main

import (
	"fmt"
)

type Stack struct {
	items []int
}

func (s *Stack) Push(val int) {
	s.items = append(s.items, val)
}

func (s *Stack) Print() {
	for i := len(s.items) - 1; i >= 0; i-- {
		fmt.Print(s.items[i])
		if i != 0 {
			fmt.Print(" -> ")
		}
	}
	fmt.Println(" -> null")
}

func main() {
	stack := Stack{}
	stack.Push(10)
	stack.Push(20)
	stack.Push(30)
	stack.Print()
}
A10 -> 20 -> 30 -> null
Bnull -> 10 -> 20 -> 30
C30 -> 20 -> 10 -> null
D30 -> 10 -> 20 -> null
Attempts:
2 left
💡 Hint
Remember that stack is Last In First Out (LIFO). The last pushed element is on top.
❓ Predict Output
intermediate
2:00remaining
What happens if you push on a nil stack slice?
In Go, what will be the output of the following code when pushing 5 onto a nil slice stack and then printing?
DSA Go
package main

import "fmt"

type Stack struct {
	items []int
}

func (s *Stack) Push(val int) {
	s.items = append(s.items, val)
}

func (s *Stack) Print() {
	for i := len(s.items) - 1; i >= 0; i-- {
		fmt.Print(s.items[i])
		if i != 0 {
			fmt.Print(" -> ")
		}
	}
	fmt.Println(" -> null")
}

func main() {
	var stack Stack
	stack.Push(5)
	stack.Print()
}
A5 -> null
Bnull
C0 -> null
Dpanic: runtime error
Attempts:
2 left
💡 Hint
Appending to a nil slice in Go creates a new slice with the element.
🔧 Debug
advanced
2:00remaining
Why does this push method cause a runtime error?
Identify the error in this push method for a stack implemented with a slice in Go.
DSA Go
func (s *Stack) Push(val int) {
	var newItems []int
	newItems[0] = val
	s.items = append(newItems, s.items...)
}
Aappend syntax is wrong, should be s.items, newItems...
BnewItems is nil and assigning newItems[0] causes an index out of range panic
CPush method should not use append, it should use copy instead
Ds.items is not initialized, causing nil pointer dereference
Attempts:
2 left
💡 Hint
Check how slices are initialized and indexed before assignment.
🧠 Conceptual
advanced
1:30remaining
What is the time complexity of the push operation on a stack implemented with a slice in Go?
Consider a stack implemented using a Go slice. What is the average time complexity of the Push operation?
AO(1) amortized time
BO(n) always
CO(n^2)
DO(log n)
Attempts:
2 left
💡 Hint
Appending to a slice sometimes requires resizing the underlying array.
🚀 Application
expert
2:30remaining
After pushing elements, what is the state of the stack?
Given the following Go code that pushes elements onto a stack, what is the printed output?
DSA Go
package main

import "fmt"

type Stack struct {
	items []int
}

func (s *Stack) Push(val int) {
	s.items = append(s.items, val)
}

func (s *Stack) Print() {
	for i := len(s.items) - 1; i >= 0; i-- {
		fmt.Print(s.items[i])
		if i != 0 {
			fmt.Print(" -> ")
		}
	}
	fmt.Println(" -> null")
}

func main() {
	stack := Stack{}
	stack.Push(1)
	stack.Push(2)
	stack.Push(3)
	stack.Push(4)
	stack.Push(5)
	stack.Print()
}
A1 -> 3 -> 5 -> 2 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
Cnull
D5 -> 4 -> 3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Remember the stack is LIFO, so the last pushed element is on top.

Practice

(1/5)
1. What does the push operation do in a stack?
easy
A. Returns the bottom element of the stack
B. Removes the top element from the stack
C. Adds an element to the top of the stack
D. Checks if the stack is empty

Solution

  1. Step 1: Understand stack behavior

    A stack follows Last In First Out (LIFO) principle, so new elements are added on top.
  2. Step 2: Define push operation

    The push operation adds a new element to the top of the stack.
  3. Final Answer:

    Adds an element to the top of the stack -> Option C
  4. Quick Check:

    Push = Add top [OK]
Hint: Push means add on top of stack [OK]
Common Mistakes:
  • Confusing push with pop operation
  • Thinking push removes elements
  • Assuming push accesses bottom element
2. Which of the following is the correct syntax to push an element 5 onto a stack named s in Go?
easy
A. s.push(5)
B. s.Push(5)
C. push(s, 5)
D. s = append(s, 5)

Solution

  1. Step 1: Understand Go slice as stack

    In Go, stacks are often implemented using slices. To push, append adds element at the end.
  2. Step 2: Identify correct syntax

    Using append(s, 5) adds 5 to the slice s, simulating push.
  3. Final Answer:

    s = append(s, 5) -> Option D
  4. Quick Check:

    Go push = append slice [OK]
Hint: Use append to push in Go slices [OK]
Common Mistakes:
  • Using lowercase push method which doesn't exist
  • Calling push as a function without method
  • Ignoring that slices need reassignment after append
3. What will be the output after executing this Go code?
stack := []int{1, 2, 3}
stack = append(stack, 4)
fmt.Println(stack)
medium
A. [1 2 3 4]
B. [4 1 2 3]
C. [1 2 3]
D. runtime error

Solution

  1. Step 1: Initial stack state

    Stack slice starts as [1 2 3].
  2. Step 2: Append 4 to stack

    Append adds 4 at the end, so stack becomes [1 2 3 4].
  3. Final Answer:

    [1 2 3 4] -> Option A
  4. Quick Check:

    Append adds at end [OK]
Hint: Append adds element at slice end [OK]
Common Mistakes:
  • Expecting append to add at start
  • Confusing append with insert
  • Thinking append causes error without reassignment
4. Identify the error in this Go code that tries to push an element onto a stack:
var stack []int
stack.push(10)
medium
A. Stack is not initialized
B. push method does not exist for slices
C. Missing import for stack package
D. Cannot push integer 10

Solution

  1. Step 1: Check stack type and methods

    Stack is a slice of int. Slices do not have a push method in Go.
  2. Step 2: Identify correct push approach

    To push, use append function instead of push method.
  3. Final Answer:

    push method does not exist for slices -> Option B
  4. Quick Check:

    Slices lack push method [OK]
Hint: Slices use append, no push method [OK]
Common Mistakes:
  • Assuming push is built-in method for slices
  • Thinking stack must be initialized first
  • Believing import fixes method absence
5. Given a stack implemented as a slice in Go, how would you push multiple elements 7, 8, 9 in one operation?
hard
A. stack = append(stack, []int{7, 8, 9}...)
B. stack.push(7); stack.push(8); stack.push(9)
C. append(stack, 7, 8, 9)
D. stack = append(stack, []int{7, 8, 9})

Solution

  1. Step 1: Understand appending multiple elements

    To add multiple elements from a slice, use the ... operator to unpack them.
  2. Step 2: Identify correct append syntax

    Using append(stack, []int{7, 8, 9}...) adds all three elements individually.
  3. Final Answer:

    stack = append(stack, []int{7, 8, 9}...) -> Option A
  4. Quick Check:

    Use ... to unpack slice in append [OK]
Hint: Use ... to append multiple elements at once [OK]
Common Mistakes:
  • Forgetting to assign the result of append back to stack
  • Using non-existent push method multiple times
  • Appending slice without unpacking elements
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