Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of direct array manipulation vs stack abstraction
Consider the following Go code snippets. What is the printed output after all operations?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 3} arr = append(arr, 4) arr = arr[:len(arr)-1] // remove last element fmt.Println(arr) stack := []int{1, 2, 3} stack = push(stack, 4) stack, _ = pop(stack) fmt.Println(stack) } func push(s []int, val int) []int { return append(s, val) } func pop(s []int) ([]int, int) { if len(s) == 0 { return s, -1 } val := s[len(s)-1] s = s[:len(s)-1] return s, val }
Attempts:
2 left
💡 Hint
Think about how the last element is removed in both cases.
✗ Incorrect
The array slice removes the last element by slicing off the last index. The stack abstraction uses push and pop functions that add and remove the last element respectively. Both end up with the original slice [1 2 3].
🧠 Conceptual
intermediateWhy use stack abstraction instead of direct array use?
Which of the following is the main reason to use a stack abstraction over direct array manipulation?
Attempts:
2 left
💡 Hint
Think about what stack abstraction controls compared to raw arrays.
✗ Incorrect
Stack abstraction enforces Last-In-First-Out (LIFO) behavior and hides direct index manipulation, preventing errors and misuse.
🔧 Debug
advancedIdentify the error in this stack implementation using array
What error will this Go code produce when run?
DSA Go
package main import "fmt" func main() { stack := []int{} stack, val := pop(stack) fmt.Println(val) } func pop(s []int) ([]int, int) { val := s[len(s)-1] s = s[:len(s)-1] return s, val }
Attempts:
2 left
💡 Hint
What happens if you try to access the last element of an empty slice?
✗ Incorrect
The code tries to access s[len(s)-1] without checking if s is empty, causing an index out of range runtime error.
🚀 Application
advancedStack abstraction benefits in multi-threaded environment
Which benefit does stack abstraction provide when multiple threads access the data structure?
Attempts:
2 left
💡 Hint
Think about how abstraction helps manage access in concurrent situations.
✗ Incorrect
Stack abstraction encapsulates operations, making it easier to add locks or synchronization mechanisms to prevent race conditions.
❓ Predict Output
expertOutput after mixed direct array and stack abstraction operations
What is the final printed output of this Go program?
DSA Go
package main import "fmt" func main() { arr := []int{10, 20, 30} arr = append(arr, 40) arr = arr[:len(arr)-1] stack := []int{10, 20, 30} stack = push(stack, 40) stack, _ = pop(stack) arr = append(arr, 50) stack = push(stack, 50) fmt.Println("Array:", arr) fmt.Println("Stack:", stack) } func push(s []int, val int) []int { return append(s, val) } func pop(s []int) ([]int, int) { if len(s) == 0 { return s, -1 } val := s[len(s)-1] s = s[:len(s)-1] return s, val }
Attempts:
2 left
💡 Hint
Track carefully the elements added and removed in both array and stack slices.
✗ Incorrect
The array removes 40 after adding it, then adds 50. The stack pushes 40 then pops it, then pushes 50. Both end with [10 20 30 50].
