How to implement 3 stacks with one array?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Implementing three stacks in one array is a classic data-structure exercise because it forces you to manage shared storage explicitly. The best implementation depends on whether you want simplicity or flexibility: a fixed partition is easy to code, while a dynamic layout uses space better at the cost of more bookkeeping.
Fixed Partition Design
The simplest design is to divide the array into three equal sections and dedicate one section to each stack. Each stack keeps its own size or top pointer.
This is the usual interview baseline because the indexing logic is straightforward.
Why Fixed Partitions Are Attractive
This design has clear strengths:
- constant-time
push,pop, andpeek - easy indexing math
- simple correctness reasoning
If each stack has a known maximum capacity, fixed partitioning is often good enough in real code too.
The Main Weakness
The downside is wasted space. One stack can fill up and overflow even when the other two stacks are mostly empty. That is the tradeoff for simplicity.
For example, if each partition has capacity 10, then stack 0 cannot grow past 10 items even if stacks 1 and 2 together only use two total slots.
A More Flexible Design
If you want the three stacks to share space dynamically, you need extra metadata. One practical way is to store nodes in one array and maintain linked-list-style stack tops plus a free-list.
This uses one physical array more efficiently because any stack can consume any free slot.
Tradeoffs Between the Two Approaches
The fixed partition method is better when:
- capacities are known in advance
- implementation simplicity matters most
- predictability is more important than perfect space efficiency
The dynamic shared-space method is better when:
- stack growth is uneven
- total capacity matters more than per-stack boundaries
- you are willing to manage extra bookkeeping
Both still support constant-time stack operations.
Validating Stack Numbers
No matter which design you choose, validate the stack identifier before indexing internal arrays.
This is a small detail, but it prevents silent corruption in implementations that assume exactly three stacks.
Common Pitfalls
The biggest pitfall in fixed partitions is off-by-one indexing. Stack boundaries are easy to get wrong if the top index calculation is not carefully defined.
Another issue is forgetting the wasted-space tradeoff. A correct fixed-partition implementation can still be the wrong design if one stack grows much faster than the others.
In the dynamic design, the main risk is corrupting the free-list or next pointers during push and pop. That kind of bug can make one mistake spread through the whole structure.
Finally, do not overengineer the problem. If the exercise or real system only needs a simple bounded solution, the fixed partition design is often the right answer.
Summary
- The simplest implementation divides one array into three fixed regions.
- Fixed partitions are easy to reason about but can waste space.
- A dynamic shared-space design uses extra metadata to let all three stacks share capacity.
- Both approaches can support constant-time stack operations.
- Choose the design based on whether simplicity or flexible space usage matters more.
Related reading
- How to implement a better sliding window algorithm?
- How to implement a binary tree?
- How to implement a Digg-like algorithm?
- How to implement a double linked list with only one pointer?
- How to implement a Map with multiple keys?
- How to implement a Median-heap
- How to implement a Least Frequently Used LFU cache?
- How to implement a queue with three stacks?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.