Implement Stack from Scratch
Pattern: Implementation
A stack with a fixed capacity chosen when it is created. Keep an array and a count. Push fails (returns false) when the array is full: that is overflow. Pop and top on an empty stack return -1: underflow.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Build FixedStack(capacity) with push(x) returning true, or false when the stack is full, pop(), top() and
size(). Pop and top return -1 on an empty stack.
The catalogue has no practice link for this one; the rules above are this guide's own.
Think first
- What two error cases does a fixed stack have?
- How do you know it is full?
- Which slot is the top?
Hints
Brute force, and why it fails
There is no slower version worth writing; the point is handling the two limits cleanly.
The key insight
A fixed block of memory and one counter are a complete stack. All the work is checking the limits before touching the array.
Dry run
| count | 3 |
|---|
FixedStack(3). push(1), push(2), push(3) all succeed.
The plan
push(x): if count == capacity: return false
data[count] = x; count += 1; return true
pop(): if count == 0: return -1
count -= 1; return data[count]
top(): return -1 if count == 0 else data[count - 1]
size(): return count
Complexity
- Time
O(1) per operation- Space
O(capacity)
One slot per operation; the array is allocated once.
Edge cases
- Capacity 1.
- Push after an overflow: the stack is unchanged.
- Pop until empty, then pop again.
I've built my logic, show the code
Java
class FixedStack {
private final int[] data;
private int count = 0;
public FixedStack(int capacity) {
data = new int[capacity]; // a fixed block of memory, as in a real "from scratch" stack
}
public boolean push(int x) {
if (count == data.length) return false; // overflow: the stack is full
data[count++] = x;
return true;
}
public int pop() {
return count == 0 ? -1 : data[--count]; // underflow gives -1
}
public int top() {
return count == 0 ? -1 : data[count - 1];
}
public int size() {
return count;
}
}C++
class FixedStack {
public:
vector<int> data;
int count = 0;
FixedStack(int capacity) : data(capacity) {} // a fixed block of memory
bool push(int x) {
if (count == static_cast<int>(data.size())) return false; // overflow: the stack is full
data[count++] = x;
return true;
}
int pop() {
return count == 0 ? -1 : data[--count]; // underflow gives -1
}
int top() {
return count == 0 ? -1 : data[count - 1];
}
int size() {
return count;
}
};Python
class FixedStack:
def __init__(self, capacity: int):
self.data = [0] * capacity # a fixed block of memory
self.count = 0
def push(self, x: int) -> bool:
if self.count == len(self.data):
return False # overflow: the stack is full
self.data[self.count] = x
self.count += 1
return True
def pop(self) -> int:
if self.count == 0:
return -1 # underflow gives -1
self.count -= 1
return self.data[self.count]
def top(self) -> int:
return -1 if self.count == 0 else self.data[self.count - 1]
def size(self) -> int:
return self.countCommon mistakes
- Writing
data[count]before checking the capacity, which goes out of bounds. - Returning
data[count]aftercount -= 1in the wrong order.