DSA
    beginner

    Stack Data Structure

    Stack (LIFO)

    O(1) ops
    Ready

    Stack Structure

    Ready to perform stack operations

    Empty Stack
    [7]
    [6]
    [5]
    [4]
    [3]
    [2]
    [1]
    [0]
    Stack
    Size: 0/8 | Top: null
    Stack Element
    Top Element
    Active Operation
    Size
    0
    Top Element
    null
    Capacity
    8
    Free Space
    8

    Last in, first out

    Sequence

    Run push 1, push 2, push 3, pop, push 4, pop, pop, pop. Order the values as they are popped.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    4 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    Which element is removed first in a Stack?

    Write the code

    Runs your solution

    Valid Parentheses

    Determine if an input string containing "()[]{}" is valid. Open brackets must be closed by the same type and in correct order.

    Example Cases

    Input:["()"]
    Output:true
    Input:["()[]{}"]
    Output:true
    Input:["(]"]
    Output:false
    Input:["([)]"]
    Output:false
    javascript

    The short version

    Think of a stack of plates in a cafeteria. You can only add a new plate to the top, and you can only take the top plate off. The last one put on is the first one taken off (LIFO).

    In more depth

    Last In, First Out (LIFO)

    Think of a stack of plates. You put a new plate on top, and you take the top plate off. You can't reach the bottom plate without removing all the ones above it.

    This simple LIFO property makes Stacks essential for:

    1.Function Calls (Recursion): Your computer uses a stack to remember where to return after a function finishes.
    2.Undo/Redo: Ctrl+Z pops the last action from the stack.
    3.Parsing: Checking if parentheses are balanced.

    When to reach for this

    • Processing nested structures (Parentheses, HTML tags)
    • Reversing data (String reverse, number base conversion)
    • Undo/Redo functionality
    • Expression evaluation (Calculator)

    Interview Questions

    Google
    Microsoft
    Bloomberg
    Amazon
    Uber
    Facebook
    Apple
    LinkedIn
    Microsoft
    Google
    Amazon
    DoorDash

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required