Good evening! Here's our prompt for today.
Recall that a stack is an abstract data type modeling a collection of elements. Its primary operations are push (which adds an element to the top of the stack) and pop (which removes the most newest element).
Traditionally, a stack can easily be implemented in many dynamic languages using an array (and its built-in methods).
1stack := []int{}
2
3stack = append(stack, 5)
4stack = append(stack, 6)
5stack = append(stack, 7)
6fmt.Println(stack[len(stack)-1]) // Prints 7.
7stack = stack[:len(stack)-1] // pops the top(the last) element
8// Output: 7
9fmt.Println(stack[len(stack)-1]) // Prints 6.
10stack = stack[:len(stack)-1] // pops the top(the last) element
11// Output: 6However, let's say we wanted to implement a stack with the following interface, requiring the following methods to be defined. The most important being the last one, min() - a method that lets us obtain the minimum element at any given time.
push(val) - add an element on to the top of the stack.
pop(val) - remove the element at the top of the stack and return it.
peek(val) - see the element at the top of the stack without removing it.
min() - get minimum element in stack.

How would you do it, and can you implement it via a MinStack class? The class should have the following methods. Work off this skeleton:
1class MinStack {
2 constructor() {
3 }
4
5 push(val) {
6 }
7
8 pop() {
9 }
10
11 peek() {
12 }
13
14 min() {
15 }
16}Can you do call min() and retrieve it in O(1) time?
Constraints
- Total number of operations in the stack <=
100000 - The values will be in the range
-1000000000and1000000000 - Expected time complexity : O(1)
- Expected space complexity : O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx​package main​import ( "fmt")​type MinStack struct { elements, min []int l int}​func (this *MinStack) Push(x int) { // fill in this method}​func (this *MinStack) Pop() { // fill in this method}​func (this *MinStack) GetMin() int { // fill in this function return 0}​func main() { min := MinStack{} min.Push(4) min.Push(7) min.Push(3)We'll now take you through what you need to know.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

