Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Stack Concept and LIFO Principle 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 operation sequence?
Consider a stack initially empty. We perform these operations in order:
push(10), push(20), pop(), push(30), pop(), push(40), push(50), pop()
What is the content of the stack from top to bottom after these operations?
DSA Typescript
class Stack {
  private items: number[] = [];
  push(item: number) { this.items.push(item); }
  pop() { 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.pop();
stack.push(40);
stack.push(50);
stack.pop();
console.log(stack.print());
A50 -> 40 -> 10 -> null
B40 -> 10 -> null
C40 -> 30 -> 10 -> null
D30 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember pop removes the top item, and push adds on top.
🧠 Conceptual
intermediate
1:00remaining
Which property best describes a stack?
A stack is a data structure that follows which principle?
ASorted Order
BFirst In, First Out (FIFO)
CRandom Access
DLast In, First Out (LIFO)
Attempts:
2 left
💡 Hint
Think about which element is removed first.
🔧 Debug
advanced
1:30remaining
What error occurs when popping from an empty stack?
Given this TypeScript stack class, what happens if pop() is called on an empty stack?
DSA Typescript
class Stack {
  private items: number[] = [];
  push(item: number) { this.items.push(item); }
  pop() { return this.items.pop(); }
}

const stack = new Stack();
stack.pop();
AReturns undefined without error
BThrows RangeError
CThrows TypeError
DThrows ReferenceError
Attempts:
2 left
💡 Hint
Check what Array.pop() returns when empty.
🚀 Application
advanced
2:00remaining
How to reverse a string using a stack?
Which code snippet correctly reverses the string 'hello' using a stack?
APop chars first, then push to reverse
BPush all chars, then pop only half to reverse
CPush each char, then pop all to build reversed string
DUse stack to sort chars alphabetically
Attempts:
2 left
💡 Hint
Think about pushing all characters first, then popping to reverse order.
❓ Predict Output
expert
2:30remaining
What is the final stack content after this complex sequence?
Given this sequence of stack operations, what is the final stack content from top to bottom?
push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5), push(6), pop()
DSA Typescript
class Stack {
  private items: number[] = [];
  push(item: number) { this.items.push(item); }
  pop() { return this.items.pop(); }
  print() { return this.items.slice().reverse().join(' -> ') + ' -> null'; }
}

const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop();
stack.push(4);
stack.pop();
stack.pop();
stack.push(5);
stack.push(6);
stack.pop();
console.log(stack.print());
A5 -> 1 -> null
B6 -> 5 -> 1 -> null
C2 -> 1 -> null
D1 -> null
Attempts:
2 left
💡 Hint
Trace each push and pop carefully to see what remains.

Practice

(1/5)
1. What does the LIFO principle mean in the context of a stack?
easy
A. Last In, First Out - the last element added is the first to be removed
B. Last In, First Out - the first element added is the first to be removed
C. First In, First Out - the first element added is the first to be removed
D. First In, Last Out - the first element added is the last to be removed

Solution

  1. Step 1: Understand LIFO meaning

    LIFO means the last element added to the stack is the first one to be removed.
  2. Step 2: Match with options

    Last In, First Out - the last element added is the first to be removed correctly describes this behavior as "Last In, First Out".
  3. Final Answer:

    Last In, First Out - the last element added is the first to be removed -> Option A
  4. Quick Check:

    LIFO = Last In, First Out [OK]
Hint: Remember stack removes newest item first [OK]
Common Mistakes:
  • Confusing LIFO with FIFO
  • Thinking first added is removed first
  • Mixing up the order of insertion and removal
2. Which of the following is the correct TypeScript syntax to push an element `5` onto a stack array named stack?
easy
A. stack.push(5);
B. stack.add(5);
C. stack.insert(5);
D. stack.append(5);

Solution

  1. Step 1: Recall array method to add element at end

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

    Only stack.push(5); uses push(5), which is correct syntax to add 5 to the stack.
  3. Final Answer:

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

    Use push() to add element to stack [OK]
Hint: Use push() to add elements to stack [OK]
Common Mistakes:
  • Using non-existent methods like add or insert
  • Confusing push with append (not in JS/TS)
  • Syntax errors like missing parentheses
3. What will be the output of the following TypeScript code?
const stack: number[] = [];
stack.push(10);
stack.push(20);
stack.push(30);
const popped = stack.pop();
console.log(stack);
medium
A. [10, 20, 30]
B. [20, 30]
C. [10, 20]
D. [30]

Solution

  1. Step 1: Trace push operations

    Elements 10, 20, and 30 are pushed in order, so stack becomes [10, 20, 30].
  2. Step 2: Trace pop operation

    Pop removes the last element (30), so stack becomes [10, 20].
  3. Final Answer:

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

    Pop removes last item, stack left with [10, 20] [OK]
Hint: Pop removes last pushed element [OK]
Common Mistakes:
  • Thinking pop removes first element
  • Confusing push and pop effects
  • Expecting stack unchanged after pop
4. Identify the error in the following TypeScript stack code:
const stack: number[] = [];
stack.push(1);
stack.push(2);
const removed = stack.pop;
console.log(removed);
medium
A. console.log syntax is wrong
B. stack array is not initialized
C. push method is used incorrectly
D. pop method is not called with parentheses

Solution

  1. Step 1: Check pop usage

    The code assigns stack.pop without parentheses, so it assigns the function itself, not the popped value.
  2. Step 2: Correct usage

    To pop and get the value, it should be stack.pop() with parentheses to call the method.
  3. Final Answer:

    pop method is not called with parentheses -> Option D
  4. Quick Check:

    Call pop() with () to execute [OK]
Hint: Always use parentheses to call pop() [OK]
Common Mistakes:
  • Forgetting parentheses on pop()
  • Confusing method reference with method call
  • Assuming pop returns function instead of value
5. Given a stack implemented as an array, which sequence of operations will result in the stack containing [1, 2, 3] after all operations?

Choose the correct sequence:
A) push(1), push(2), pop(), push(3), push(2)
B) push(1), pop(), push(2), push(3), push(1)
C) push(3), push(2), push(1), pop(), push(1)
D) push(1), push(2), push(3), pop(), push(3)
hard
A. push(1), push(2), pop(), push(3), push(2)
B. push(1), push(2), push(3), pop(), push(3)
C. push(3), push(2), push(1), pop(), push(1)
D. push(1), pop(), push(2), push(3), push(1)

Solution

  1. Step 1: Trace push(1), push(2), push(3), pop(), push(3) operations

    Start with empty stack []:
    push(1) -> [1]
    push(2) -> [1, 2]
    push(3) -> [1, 2, 3]
    pop() removes 3 -> [1, 2]
    push(3) -> [1, 2, 3]
  2. Step 2: Verify final stack

    After these operations, stack is [1, 2, 3], matching the target.
  3. Final Answer:

    push(1), push(2), push(3), pop(), push(3) -> Option B
  4. Quick Check:

    Pop removes last, push adds correct element [OK]
Hint: Trace each push/pop step carefully [OK]
Common Mistakes:
  • Not tracking pop effect correctly
  • Assuming pop removes first element
  • Mixing order of pushes and pops
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