GamifiedGamified challengesPracticeRaceLeaderboardPricing

Learn data structures by watching them move, then doing it yourself. Press M anytime to toggle sound.

A product of Amypo Technologies, Coimbatore.

Learn

  • All topics
  • Learning path
  • How it works
  • About dotprep

Practice

  • Practice arena
  • Daily challenge
  • Gamified challenges
  • Race mode
  • Leaderboard

Interview prep

  • Java interview
  • Coding questions
  • Company questions
  • System design
  • Jobs & careers

You

  • Dashboard
  • Settings
  • Pricing
  • Create account

© 2026 dotprep

Content last updated 20 November 2026

GamifiedGamified challengesPracticeRaceLeaderboardPricing
Log inStart free

Free preview: 3 of 3 free topics used. You can come back to these anytime; other topics need a free account.

Sign up free to unlock all 99
TopicsStacks and queues

Stack

beginnerpush, pop, peek: all O(1)
  • just pushed
  • top being read
empty · top = -1

An empty stack with room for 8 values. top = -1 means it's empty.

1 / 17

Operations (8/20)

  1. push(12)
  2. push(45)
  3. push(7)
  4. peek()
  5. pop()
  6. push(30)
  7. pop()
  8. pop()
1class Stack {
2 int[] data = new int[8];
3 int top = -1;
4
5 void push(int x) {
6 if (top == data.length - 1)
7 throw new RuntimeException("overflow");
8 data[++top] = x;
9 }
10
11 int pop() {
12 if (top == -1)
13 throw new RuntimeException("underflow");
14 return data[top--];
15 }
16
17 int peek() {
18 if (top == -1)
19 throw new RuntimeException("empty");
20 return data[top];
21 }
22}

Variables

top =
-1

Work done

Size
0
Operations
0

Shortcuts: Space play/pause · ← → step · M sound