Bird
Raised Fist0
DSA Javaprogramming~20 mins

Push Operation on Stack in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after pushing elements on the stack?
Consider a stack initially empty. We push the elements 5, 10, and 15 in this order. What is the printed state of the stack after these operations?
DSA Java
class Stack {
    private int[] arr = new int[10];
    private int top = -1;

    public void push(int x) {
        if (top < arr.length - 1) {
            arr[++top] = x;
        }
    }

    public void printStack() {
        for (int i = top; i >= 0; i--) {
            System.out.print(arr[i] + " -> ");
        }
        System.out.println("null");
    }
}

public class Main {
    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(5);
        stack.push(10);
        stack.push(15);
        stack.printStack();
    }
}
Anull
B5 -> 10 -> 15 -> null
C15 -> 10 -> 5 -> null
D15 -> 5 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember, stack is Last In First Out (LIFO). The last pushed element is on top.
❓ Predict Output
intermediate
2:00remaining
What happens if we push beyond stack capacity?
Given a stack with capacity 3, what is the output after pushing 1, 2, 3, and then 4?
DSA Java
class Stack {
    private int[] arr = new int[3];
    private int top = -1;

    public void push(int x) {
        if (top < arr.length - 1) {
            arr[++top] = x;
        }
    }

    public void printStack() {
        for (int i = top; i >= 0; i--) {
            System.out.print(arr[i] + " -> ");
        }
        System.out.println("null");
    }
}

public class Main {
    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.printStack();
    }
}
A4 -> 3 -> 2 -> 1 -> null
Bnull
C1 -> 2 -> 3 -> 4 -> null
D3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Check if the push method prevents adding elements beyond capacity.
🔧 Debug
advanced
2:00remaining
Identify the bug in the push method
What is the problem with this push method in the stack implementation?
DSA Java
public void push(int x) {
    if (top <= arr.length) {
        arr[++top] = x;
    }
}
AIt allows pushing beyond array size causing ArrayIndexOutOfBoundsException
BIt never pushes any element because condition is always false
CIt causes infinite loop
DIt throws NullPointerException
Attempts:
2 left
💡 Hint
Check the condition that controls pushing elements.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of push operation on a stack?
Assuming a stack implemented with an array, what is the time complexity of the push operation?
AO(n) - Linear time
BO(1) - Constant time
CO(log n) - Logarithmic time
DO(n^2) - Quadratic time
Attempts:
2 left
💡 Hint
Push adds one element at the top without looping over elements.
❓ Predict Output
expert
3:00remaining
What is the stack state after mixed push and pop operations?
Given the following operations on an empty stack: push(7), push(14), pop(), push(21), push(28), pop(), push(35). What is the printed stack state?
DSA Java
class Stack {
    private int[] arr = new int[10];
    private int top = -1;

    public void push(int x) {
        if (top < arr.length - 1) {
            arr[++top] = x;
        }
    }

    public void pop() {
        if (top >= 0) {
            top--;
        }
    }

    public void printStack() {
        for (int i = top; i >= 0; i--) {
            System.out.print(arr[i] + " -> ");
        }
        System.out.println("null");
    }
}

public class Main {
    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(7);
        stack.push(14);
        stack.pop();
        stack.push(21);
        stack.push(28);
        stack.pop();
        stack.push(35);
        stack.printStack();
    }
}
A35 -> 21 -> 7 -> null
B28 -> 21 -> 7 -> null
C21 -> 7 -> null
D35 -> 28 -> 21 -> 7 -> null
Attempts:
2 left
💡 Hint
Track each push and pop step carefully to see which elements remain.

Practice

(1/5)
1. What does the push operation do in a stack?
easy
A. Adds an element to the top of the stack
B. Removes the top element from the stack
C. Checks if the stack is empty
D. Returns the bottom element of the stack

Solution

  1. Step 1: Understand stack behavior

    A stack follows Last In First Out (LIFO) principle, so new elements are added on top.
  2. Step 2: Define push operation

    The push operation adds a new element to the top of the stack, increasing its size by one.
  3. Final Answer:

    Adds an element to the top of the stack -> Option A
  4. Quick Check:

    Push = Add top element [OK]
Hint: Push always adds element on top of stack [OK]
Common Mistakes:
  • Confusing push with pop
  • Thinking push removes element
  • Assuming push adds at bottom
2. Which of the following is the correct syntax to push an integer 10 onto a stack named stack in Java?
easy
A. stack.add(10);
B. stack.push(10);
C. stack.insert(10);
D. stack.put(10);

Solution

  1. Step 1: Recall Java Stack API

    Java's Stack class uses the method push() to add elements.
  2. Step 2: Match correct method

    Among options, only push() is the correct method to add elements to a stack.
  3. Final Answer:

    stack.push(10); -> Option B
  4. Quick Check:

    Java stack push method = push() [OK]
Hint: Use push() method to add elements in Java stack [OK]
Common Mistakes:
  • Using add() which is for lists
  • Using insert() which doesn't exist
  • Using put() which is for maps
3. What will be the output after executing the following Java code?
Stack<Integer> stack = new Stack<>();
stack.push(5);
stack.push(10);
stack.push(15);
System.out.println(stack);
medium
A. [5, 15, 10]
B. [15, 10, 5]
C. [10, 15, 5]
D. [5, 10, 15]

Solution

  1. Step 1: Trace push operations

    Elements 5, 10, and 15 are pushed in order, so stack stores them as [5, 10, 15].
  2. Step 2: Understand Stack's toString()

    Java's Stack prints elements from bottom to top in list format, so output is [5, 10, 15].
  3. Final Answer:

    [5, 10, 15] -> Option D
  4. Quick Check:

    Stack print shows bottom to top [OK]
Hint: Stack prints bottom to top in Java [OK]
Common Mistakes:
  • Assuming print shows top element first
  • Confusing stack with queue order
  • Expecting reversed list output
4. Identify the error in the following code snippet that tries to push an element onto a stack:
Stack<String> stack = new Stack<>();
stack.push("Hello");
stack.push(100);
medium
A. Stack must be initialized with size
B. Stack class does not have push method
C. Cannot push integer 100 into a Stack of Strings
D. Missing semicolon after push statements

Solution

  1. Step 1: Check stack type and pushed elements

    The stack is declared to hold Strings only, but 100 is an integer.
  2. Step 2: Identify type mismatch error

    Pushing an integer into a String stack causes a compile-time type error.
  3. Final Answer:

    Cannot push integer 100 into a Stack of Strings -> Option C
  4. Quick Check:

    Type mismatch error = pushing wrong data type [OK]
Hint: Stack type must match pushed element type [OK]
Common Mistakes:
  • Ignoring generic type mismatch
  • Thinking push method missing
  • Assuming size needed at init
5. Given an empty stack, which sequence of push operations will result in the stack containing elements in this order from bottom to top: 3 -> 7 -> 5 -> null?
hard
A. push(3), push(7), push(5)
B. push(5), push(7), push(3)
C. push(7), push(3), push(5)
D. push(5), push(3), push(7)

Solution

  1. Step 1: Understand stack order after pushes

    Elements pushed first are at bottom, last pushed at top.
  2. Step 2: Match push order to desired stack

    To get 3 at bottom, then 7, then 5 on top, push in order: 3, then 7, then 5.
  3. Final Answer:

    push(3), push(7), push(5) -> Option A
  4. Quick Check:

    Push order = bottom to top [OK]
Hint: Push elements in bottom-to-top order [OK]
Common Mistakes:
  • Reversing push order
  • Confusing push with pop order
  • Assuming stack inserts at bottom
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard