Stack Data Structure Java | Fresher Interview
Fast recall
Quick revision
Core answer
Stack class.Whether you know the LIFO principle, the O(1) operations, and the preferred Java class
Recommending the legacy synchronized Stack class, or forgetting peek vs pop distinction
Use ArrayDeque (not Stack), and name real uses: call stack, undo, DFS, expression parsing
Interview-ready guide
Interview answer
A stack is a last-in, first-out data structure, LIFO for short.
That means the most recently added element is the first one to come out, and all the action happens at one end, which we call the top. The classic mental model is a stack of plates: you place a new plate on top, and when you take one, you take it off the top, so the last plate you added is the first you remove.
The core operations are push, which adds an element to the top; pop, which removes and returns the top element; peek, sometimes called top, which lets you look at the top element without removing it; and isEmpty, which tells you whether the stack has any elements. All of these are O of one, constant time, because they only touch the top. In Java, the thing I'd emphasize is which class to use.
There's a legacy Stack class, but I avoid it, because it extends Vector, it's synchronized so it's slower, and it exposes index-based access that breaks the clean stack abstraction. Instead I use a Deque, specifically ArrayDeque, with push, pop, and peek.
A couple of things to be careful about: peek only looks while pop removes, so mixing them up changes behavior, and calling pop on an empty ArrayDeque throws an exception, so I check isEmpty first. Stacks show up everywhere: the program call stack itself, undo and redo features, depth-first search and backtracking, and parsing or evaluating expressions like checking balanced parentheses.
Stack Data Structure Java | Fresher Interview
Direct Answer
A stack is a LIFO (Last-In-First-Out) structure with three core operations: push (add to top), pop (remove from top), and peek/top (look at the top without removing), plus isEmpty. All are O(1). Think of a stack of plates - you add and remove from the same end. In Java, prefer ArrayDeque over the legacy Stack class.
Code Example
Deque<Integer> stack = new ArrayDeque<>(); // preferred over Stack
stack.push(10); // top -> 10
stack.push(20); // top -> 20, then 10
stack.push(30); // top -> 30, 20, 10
System.out.println(stack.peek()); // 30 (look, don't remove)
System.out.println(stack.pop()); // 30 (remove and return)
System.out.println(stack.pop()); // 20
System.out.println(stack.isEmpty()); // false (10 remains)
push/pop/peek all operate on the same end (top) in O(1).
Common Mistakes & Pitfalls
- Using legacy
Stack: it extendsVector, is synchronized (slower), and leaks index access - useArrayDeque. - peek vs pop:
peekonly looks;popremoves. Mixing them up corrupts logic. - pop on empty:
ArrayDeque.pop()throwsNoSuchElementException- checkisEmpty()first. - Confusing with queue: stack is LIFO; queue is FIFO.
Check your understanding
Answer these without looking back at the article.
- How do you validate balanced parentheses using a stack?
- What is the difference between stack and queue?
- How does the JVM call stack work?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.