Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Stack Implementation Using Array in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of the stack after these operations?
Consider a stack implemented using an array with the following operations:

push(10), push(20), pop(), push(30), push(40), pop()

What is the printed state of the stack (top to bottom) after these operations?
DSA Javascript
class Stack {
  constructor() {
    this.items = [];
  }
  push(element) {
    this.items.push(element);
  }
  pop() {
    if (this.items.length === 0) return null;
    return this.items.pop();
  }
  print() {
    return this.items.slice().reverse().join(' -> ') + ' -> null';
  }
}

const stack = new Stack();
stack.push(10);
stack.push(20);
stack.pop();
stack.push(30);
stack.push(40);
stack.pop();
console.log(stack.print());
A30 -> 10 -> null
B20 -> 10 -> null
C10 -> 30 -> null
D40 -> 30 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember pop removes the top element from the stack.
🧠 Conceptual
intermediate
1:00remaining
Which operation is NOT typical for a stack?
Stacks usually support push, pop, and peek operations. Which of the following operations is NOT a standard stack operation?
Apush(element)
Bpop()
Cpeek()
Denqueue(element)
Attempts:
2 left
💡 Hint
Think about which operation is typical for queues, not stacks.
🔧 Debug
advanced
1:30remaining
What error does this stack implementation cause?
Consider this stack pop method:

pop() {
return this.items.pop();
}

What error or issue can occur if pop is called on an empty stack?
DSA Javascript
class Stack {
  constructor() {
    this.items = [];
  }
  pop() {
    return this.items.pop();
  }
}
AThrows TypeError
BThrows RangeError
CReturns undefined without error
DThrows ReferenceError
Attempts:
2 left
💡 Hint
What does JavaScript's array pop() return when empty?
❓ Predict Output
advanced
2:00remaining
What is the output after pushing and popping multiple elements?
Given the following code, what is the printed stack state (top to bottom)?
DSA Javascript
class Stack {
  constructor() {
    this.items = [];
  }
  push(element) {
    this.items.push(element);
  }
  pop() {
    if (this.items.length === 0) return null;
    return this.items.pop();
  }
  print() {
    return this.items.slice().reverse().join(' -> ') + ' -> null';
  }
}

const stack = new Stack();
stack.push(5);
stack.push(15);
stack.push(25);
stack.pop();
stack.push(35);
stack.pop();
stack.pop();
console.log(stack.print());
A35 -> 15 -> 5 -> null
B5 -> null
C25 -> 15 -> 5 -> null
D15 -> 5 -> null
Attempts:
2 left
💡 Hint
Track each push and pop carefully.
🚀 Application
expert
1:30remaining
How many items remain after this sequence of stack operations?
A stack starts empty. The following operations occur:

push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5)

How many items are in the stack at the end?
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
Count pushes and pops step by step.

Practice

(1/5)
1. What is the main characteristic of a stack data structure implemented using an array?
easy
A. Sorted Order
B. Last In, First Out (LIFO)
C. Random Access
D. First In, First Out (FIFO)

Solution

  1. Step 1: Understand stack behavior

    A stack adds and removes elements from the same end, so the last element added is the first to be removed.
  2. Step 2: Identify the correct term

    This behavior is called Last In, First Out (LIFO).
  3. Final Answer:

    Last In, First Out (LIFO) -> Option B
  4. Quick Check:

    Stack = LIFO [OK]
Hint: Stack always removes the last added item first [OK]
Common Mistakes:
  • Confusing stack with queue (FIFO)
  • Thinking stack allows random access
  • Assuming stack keeps elements sorted
2. Which of the following is the correct way to add an element to a stack implemented as an array in JavaScript?
easy
A. stack.push(element);
B. stack.pop(element);
C. stack.shift(element);
D. stack.unshift(element);

Solution

  1. Step 1: Identify method to add element at the end

    In JavaScript arrays, push() adds an element to the end of the array.
  2. Step 2: Confirm stack push operation

    Stack push means adding element on top, which corresponds to array's end, so push() is correct.
  3. Final Answer:

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

    Use push() to add element to stack [OK]
Hint: Use push() to add on top of stack [OK]
Common Mistakes:
  • Using pop() which removes element
  • Using shift() or unshift() which affect start of array
3. What will be the output of the following code?
const stack = [];
stack.push(10);
stack.push(20);
stack.pop();
stack.push(30);
console.log(stack);
medium
A. [10, 20, 30]
B. [30]
C. [20, 30]
D. [10, 30]

Solution

  1. Step 1: Trace push and pop operations

    Initially stack = [] push(10) -> [10] push(20) -> [10, 20] pop() removes last element 20 -> [10] push(30) -> [10, 30]
  2. Step 2: Check final stack state

    After all operations, stack contains [10, 30].
  3. Final Answer:

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

    Push and pop update stack correctly [OK]
Hint: Pop removes last pushed, push adds at end [OK]
Common Mistakes:
  • Assuming pop removes first element
  • Confusing order of elements after pop
4. Identify the error in the following stack implementation code and select the correct fix:
class Stack {
  constructor() {
    this.items = [];
  }
  push(element) {
    this.items.unshift(element);
  }
  pop() {
    return this.items.pop();
  }
}
medium
A. Use push() instead of unshift() in push method
B. Use shift() instead of pop() in pop method
C. No error, code is correct
D. Use pop() instead of push() in push method

Solution

  1. Step 1: Analyze push method

    push uses unshift which adds element at start of array.
  2. Step 2: Analyze pop method

    pop removes element from end of array. This causes mismatch because push adds at start but pop removes from end.
  3. Step 3: Fix mismatch

    To maintain stack behavior, push should add at end using push(), so pop removes last added element correctly.
  4. Final Answer:

    Use push() instead of unshift() in push method -> Option A
  5. Quick Check:

    push() and pop() must match ends [OK]
Hint: Push and pop must operate on same array end [OK]
Common Mistakes:
  • Mixing unshift with pop causes wrong stack order
  • Thinking shift() and pop() are interchangeable
  • Ignoring array end consistency
5. You want to implement a stack using an array that also tracks the maximum element at any time efficiently. Which approach is best?
hard
A. Scan entire array to find max after each pop
B. Sort the array after each push to find max
C. Use two stacks: one for elements, one for max values
D. Store max element only in a variable updated on push

Solution

  1. Step 1: Understand max tracking challenge

    Finding max after each operation must be efficient, not scanning entire array.
  2. Step 2: Evaluate options

    Sorting after each push is costly. Scanning array after pop is slow. Storing max in variable fails if max is popped.
  3. Step 3: Use two stacks approach

    One stack stores elements, second stack stores current max at each push. This allows O(1) max retrieval.
  4. Final Answer:

    Use two stacks: one for elements, one for max values -> Option C
  5. Quick Check:

    Two stacks track max efficiently [OK]
Hint: Use auxiliary stack to track max values [OK]
Common Mistakes:
  • Sorting array each time is inefficient
  • Single variable max fails when max is popped
  • Scanning array after pop is slow
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