Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Stack Implementation Using Array in DSA Typescript - 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 of this stack push and pop sequence?
Consider a stack implemented using an array with a maximum size of 3. What will be the printed stack after these operations?
DSA Typescript
class Stack {
  private items: number[] = [];
  private maxSize: number = 3;

  push(element: number): boolean {
    if (this.items.length === this.maxSize) return false;
    this.items.push(element);
    return true;
  }

  pop(): number | undefined {
    return this.items.pop();
  }

  print(): string {
    return this.items.join(' -> ') + ' -> null';
  }
}

const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40); // This push should fail
stack.pop();
console.log(stack.print());
A10 -> 20 -> 30 -> null
B10 -> 20 -> null
C20 -> 30 -> null
D10 -> 20 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
Remember the stack size limit and that the last pushed element is popped first.
🧠 Conceptual
intermediate
1:00remaining
What happens when you pop from an empty stack implemented with an array?
Given a stack implemented using an array, what is the result of calling pop() when the stack is empty?
AReturns undefined or null indicating the stack is empty
BReturns the last element pushed before emptying
CThrows a runtime error immediately
DRemoves the first element pushed into the stack
Attempts:
2 left
💡 Hint
Think about what pop() does when no elements exist.
❓ Predict Output
advanced
2:00remaining
What is the output after this sequence of push and pop operations?
Analyze the following TypeScript stack code and determine the printed stack state.
DSA Typescript
class Stack {
  private items: number[] = [];
  push(element: number) {
    this.items.push(element);
  }
  pop(): number | undefined {
    return this.items.pop();
  }
  print(): string {
    return this.items.join(' -> ') + ' -> null';
  }
}

const stack = new Stack();
stack.push(5);
stack.push(10);
stack.pop();
stack.push(15);
stack.push(20);
stack.pop();
console.log(stack.print());
A5 -> 15 -> null
B5 -> 10 -> 15 -> null
C5 -> 10 -> null
D15 -> 20 -> null
Attempts:
2 left
💡 Hint
Track each push and pop carefully in order.
🔧 Debug
advanced
1:30remaining
Identify the error in this stack pop method implementation
What error will this TypeScript stack pop method cause when called on an empty stack?
DSA Typescript
class Stack {
  private items: number[] = [];
  pop(): number {
    return this.items.pop()!;
  }
}
ASyntax error because of missing return type
BNo error, returns undefined safely
CCompile-time error due to missing constructor
DRuntime error due to popping from empty array returning undefined, then using non-null assertion
Attempts:
2 left
💡 Hint
Consider what happens when pop() returns undefined but non-null assertion is used.
🚀 Application
expert
2:30remaining
How many items remain after this complex push/pop sequence on a stack with max size 4?
Given a stack with max size 4, after this sequence of operations, how many items remain in the stack?
DSA Typescript
class Stack {
  private items: number[] = [];
  private maxSize: number = 4;

  push(element: number): boolean {
    if (this.items.length === this.maxSize) return false;
    this.items.push(element);
    return true;
  }

  pop(): number | undefined {
    return this.items.pop();
  }

  size(): number {
    return this.items.length;
  }
}

const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop();
stack.push(4);
stack.push(5);
stack.push(6); // should fail
stack.pop();
stack.pop();
stack.push(7);
A5
B4
C3
D2
Attempts:
2 left
💡 Hint
Track the stack size after each operation and remember the max size limit.

Practice

(1/5)
1. What is the main characteristic of a stack data structure implemented using an array?
easy
A. It stores elements in sorted order
B. It follows First In First Out (FIFO) order
C. It allows random access to elements
D. It follows Last In First Out (LIFO) order

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 order type

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

    It follows Last In First Out (LIFO) order -> Option D
  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 sorts elements
  • Assuming random access like arrays
2. Which of the following is the correct way to push an element 'x' onto a stack implemented as an array named stack in TypeScript?
easy
A. stack.insert(x);
B. stack.pop(x);
C. stack.push(x);
D. stack.add(x);

Solution

  1. Step 1: Recall array methods for stack push

    In TypeScript, to add an element at the end of an array, use the push method.
  2. Step 2: Verify other options

    pop removes elements, insert and add are not valid array methods.
  3. Final Answer:

    stack.push(x); -> Option C
  4. Quick Check:

    Push uses .push() method [OK]
Hint: Use .push() to add elements to array stack [OK]
Common Mistakes:
  • Using pop instead of push
  • Using non-existent methods like insert or add
  • Confusing push with pop
3. What will be the output of the following TypeScript code implementing a stack using an array?
const stack: number[] = [];
stack.push(10);
stack.push(20);
stack.pop();
stack.push(30);
console.log(stack);
medium
A. [10, 20, 30]
B. [10, 30]
C. [20, 30]
D. [30]

Solution

  1. Step 1: Trace stack operations step-by-step

    Initially stack = []. After push(10), stack = [10]. After push(20), stack = [10, 20]. After pop(), top element 20 is removed, stack = [10]. After push(30), stack = [10, 30].
  2. Step 2: Determine final stack state

    The console.log prints the current stack array: [10, 30].
  3. Final Answer:

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

    Push and pop update stack correctly [OK]
Hint: Pop removes last pushed element, then push adds new [OK]
Common Mistakes:
  • Forgetting pop removes last element
  • Assuming pop removes first element
  • Confusing order of push and pop
4. Identify the error in the following TypeScript code for stack pop operation and select the correct fix.
class Stack {
  private items: number[] = [];
  pop(): number {
    return this.items.pop();
  }
}
medium
A. pop() should check if stack is empty before popping
B. items array should be public, not private
C. pop() should return void, not number
D. No error, code is correct

Solution

  1. Step 1: Analyze pop method behavior

    The pop() method of array returns number | undefined if empty. Returning directly without check can cause undefined return.
  2. Step 2: Identify fix for empty stack

    To avoid returning undefined, pop() should check if items is empty before popping and handle that case.
  3. Final Answer:

    pop() should check if stack is empty before popping -> Option A
  4. Quick Check:

    Check empty before pop to avoid undefined [OK]
Hint: Always check stack empty before pop to avoid errors [OK]
Common Mistakes:
  • Ignoring empty stack check
  • Changing return type incorrectly
  • Making items public unnecessarily
5. Given a stack implemented using an array, how would you implement a function reverseStack that reverses the elements in the stack without using any extra stack or array? Which approach is correct?
hard
A. Use recursion to pop all elements, then insert them at bottom recursively
B. Use a for loop to swap elements from start and end indices
C. Pop all elements into a new array, then push back in reverse order
D. Use a queue to dequeue and enqueue elements to reverse order

Solution

  1. Step 1: Understand constraints

    No extra stack or array allowed, so iterative swapping or extra storage is not possible.
  2. Step 2: Use recursion to reverse stack

    Recursion can pop all elements, then insert each element at the bottom of the stack recursively, effectively reversing it.
  3. Step 3: Evaluate other options

    Use a for loop to swap elements from start and end indices requires direct index access which is not typical for stack abstraction. Pop all elements into a new array, then push back in reverse order uses extra array. Use a queue to dequeue and enqueue elements to reverse order uses queue, which is extra data structure.
  4. Final Answer:

    Use recursion to pop all elements, then insert them at bottom recursively -> Option A
  5. Quick Check:

    Recursion reverses stack without extra storage [OK]
Hint: Recursion can reverse stack by inserting at bottom [OK]
Common Mistakes:
  • Using extra arrays or stacks against constraints
  • Trying to swap elements directly in stack
  • Using other data structures like queue
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