Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Push Master
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 the following TypeScript code that pushes elements onto a stack. What will be the printed stack after all push operations?
DSA Typescript
class Stack {
  private items: number[] = [];
  push(element: number) {
    this.items.push(element);
  }
  print() {
    console.log(this.items.join(' -> ') + ' -> null');
  }
}
const stack = new Stack();
stack.push(5);
stack.push(10);
stack.push(15);
stack.print();
A5 -> 10 -> 15 -> null
B15 -> 10 -> 5 -> null
Cnull -> 5 -> 10 -> 15
D10 -> 15 -> 5 -> null
Attempts:
2 left
💡 Hint
Remember that push adds elements to the end of the array, so the first pushed element is at the bottom.
❓ Predict Output
intermediate
2:00remaining
What is the stack content after pushing and popping?
Given the following TypeScript code, what will be the printed stack after the operations?
DSA Typescript
class Stack {
  private items: number[] = [];
  push(element: number) {
    this.items.push(element);
  }
  pop() {
    return this.items.pop();
  }
  print() {
    console.log(this.items.join(' -> ') + ' -> null');
  }
}
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.pop();
stack.push(3);
stack.print();
A1 -> 2 -> 3 -> null
B2 -> 3 -> null
C3 -> 1 -> null
D1 -> 3 -> null
Attempts:
2 left
💡 Hint
Pop removes the last pushed element.
🧠 Conceptual
advanced
2:00remaining
What happens internally when pushing on a stack implemented with an array?
When you push an element onto a stack implemented using an array, which of the following best describes what happens internally?
AThe array is reversed and the element is added at the start.
BThe element is inserted at the beginning of the array, shifting all other elements.
CThe element is added at the end of the array, increasing its length by one.
DThe element replaces the first element of the array.
Attempts:
2 left
💡 Hint
Think about how push works on arrays.
🔧 Debug
advanced
2:00remaining
Why does this push method fail to add elements correctly?
Look at this TypeScript stack push method. Why does it fail to add elements properly? class Stack { private items: number[] = []; push(element: number) { this.items[this.items.length] = element; this.items.length = this.items.length + 1; } }
ABecause assigning to items[items.length] is invalid and causes runtime error.
BBecause manually increasing length after assignment adds empty slots, causing incorrect stack size.
CBecause push should remove elements, not add them.
DBecause the method does not return the pushed element.
Attempts:
2 left
💡 Hint
Think about what happens when you increase array length manually.
🚀 Application
expert
2:00remaining
After pushing elements, what is the top element of the stack?
Given this TypeScript stack implementation, after pushing 7, 14, and 21, what is the value of the top element?
DSA Typescript
class Stack {
  private items: number[] = [];
  push(element: number) {
    this.items.push(element);
  }
  top() {
    return this.items[this.items.length - 1];
  }
}
const stack = new Stack();
stack.push(7);
stack.push(14);
stack.push(21);
const topElement = stack.top();
A21
B7
C14
Dundefined
Attempts:
2 left
💡 Hint
Top element is the last pushed element.

Practice

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

Solution

  1. Step 1: Understand stack behavior

    A stack follows Last In First Out (LIFO) principle, where elements are added or removed from the top.
  2. Step 2: Define push operation

    The push operation adds a new element on top of the existing elements in the stack.
  3. Final Answer:

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

    Push = Add top [OK]
Hint: Push always adds on top of stack [OK]
Common Mistakes:
  • Confusing push with pop operation
  • Thinking push removes elements
  • Mixing stack with queue operations
2. Which of the following is the correct TypeScript syntax to push an element 5 into a stack array named stack?
easy
A. stack.push(5);
B. stack.add(5);
C. push(stack, 5);
D. stack.insert(5);

Solution

  1. Step 1: Recall TypeScript array push method

    In TypeScript, arrays have a built-in method push() to add elements at the end (top for stack).
  2. Step 2: Match syntax

    The correct syntax is calling push on the array with the element as argument: stack.push(5);
  3. Final Answer:

    stack.push(5); -> Option A
  4. Quick Check:

    Array push method = stack.push(value) [OK]
Hint: Use array.push(element) to push [OK]
Common Mistakes:
  • Using non-existent methods like add or insert
  • Calling push as a standalone function
  • Forgetting parentheses after push
3. What will be the content of the stack after executing the following TypeScript code?
const stack: number[] = [];
stack.push(10);
stack.push(20);
stack.push(30);
medium
A. [30, 20, 10]
B. [10, 20, 30]
C. [10, 30, 20]
D. [20, 10, 30]

Solution

  1. Step 1: Trace each push operation

    Initially, stack is empty []. After push(10), stack = [10]. After push(20), stack = [10, 20]. After push(30), stack = [10, 20, 30].
  2. Step 2: Understand stack order

    The first pushed element is at index 0, last pushed at the end. So the array shows bottom to top as left to right.
  3. Final Answer:

    [10, 20, 30] -> Option B
  4. Quick Check:

    Push order = [10, 20, 30] [OK]
Hint: Stack array shows bottom to top left to right [OK]
Common Mistakes:
  • Reversing the order of elements
  • Confusing push with pop output
  • Assuming push adds at start of array
4. Identify the error in the following TypeScript code that tries to push an element into a stack:
let stack: number[] = [1, 2, 3];
stack.push 4;
medium
A. Missing parentheses after push
B. Stack should be declared as const
C. push cannot be used on arrays
D. Missing semicolon after array declaration

Solution

  1. Step 1: Check push method syntax

    The push method requires parentheses to pass the element: stack.push(4);. The code uses stack.push 4; which is invalid.
  2. Step 2: Verify other parts

    Declaring stack with let is valid. push is valid on arrays. Semicolon is optional but not an error here.
  3. Final Answer:

    Missing parentheses after push -> Option A
  4. Quick Check:

    push() needs parentheses [OK]
Hint: Always use parentheses with push method [OK]
Common Mistakes:
  • Omitting parentheses on method calls
  • Thinking push is a property, not a method
  • Confusing let and const errors
5. Given a stack implemented as an array, how can you push multiple elements [4, 5, 6] onto the stack in one operation while preserving their order (4 at bottom, 6 at top)?
hard
A. Use stack.unshift(...[4, 5, 6])
B. Use stack.push([4, 5, 6])
C. Use a loop to push each element from left to right
D. Use stack.push(...[4, 5, 6])

Solution

  1. Step 1: Understand spread operator with push

    Using stack.push(...[4, 5, 6]) spreads the array elements as separate arguments, pushing 4, then 5, then 6 in order.
  2. Step 2: Compare other options

    stack.push([4, 5, 6]) pushes the entire array as one element, not separate elements.
    Looping works but is multiple operations, not one.
    unshift adds elements at the start, not top of stack.
  3. Final Answer:

    Use stack.push(...[4, 5, 6]) -> Option D
  4. Quick Check:

    Spread operator with push adds multiple elements [OK]
Hint: Use spread operator ... with push for multiple elements [OK]
Common Mistakes:
  • Pushing array as single element instead of separate
  • Using unshift which adds at bottom
  • Forgetting spread operator syntax
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