Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Queue vs Stack When to Use Which in DSA Typescript - Compare & Choose

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue vs Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Choosing Between Queue and Stack for Task Scheduling

You have a list of tasks that must be processed in the exact order they arrive. Which data structure is best to use?

AQueue, because it processes tasks in the order they arrive.
BStack, because it processes the most recent task first.
CStack, because it allows random access to tasks.
DQueue, because it processes tasks in reverse order.
Attempts:
2 left
💡 Hint

Think about which data structure follows the 'first come, first served' rule.

❓ Predict Output
intermediate
2:00remaining
Output of Stack Operations

What is the printed output after running this TypeScript code that uses a stack?

DSA Typescript
class Stack<T> {
  private items: T[] = [];
  push(item: T) { this.items.push(item); }
  pop(): T | undefined { return this.items.pop(); }
  print() { console.log(this.items.join(' -> ') + ' -> null'); }
}

const stack = new Stack<number>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop();
stack.print();
A3 -> 2 -> 1 -> null
B1 -> 2 -> null
C1 -> 3 -> null
D2 -> 1 -> null
Attempts:
2 left
💡 Hint

Remember that pop removes the last pushed item.

❓ Predict Output
advanced
2:00remaining
Queue Behavior with Enqueue and Dequeue

What is the printed output after running this TypeScript code that uses a queue?

DSA Typescript
class Queue<T> {
  private items: T[] = [];
  enqueue(item: T) { this.items.push(item); }
  dequeue(): T | undefined { return this.items.shift(); }
  print() { console.log(this.items.join(' -> ') + ' -> null'); }
}

const queue = new Queue<number>();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.dequeue();
queue.print();
A10 -> 30 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
D30 -> 20 -> null
Attempts:
2 left
💡 Hint

Remember that dequeue removes the first item added.

🔧 Debug
advanced
2:00remaining
Identifying Incorrect Use of Stack for Undo Feature

Which option shows a problem when using a stack to implement an undo feature in a text editor?

DSA Typescript
class UndoStack {
  private stack: string[] = [];
  addAction(action: string) { this.stack.push(action); }
  undo(): string | undefined { return this.stack.pop(); }
  print() { console.log(this.stack.join(' -> ') + ' -> null'); }
}

const undoStack = new UndoStack();
undoStack.addAction('type A');
undoStack.addAction('type B');
undoStack.undo();
undoStack.print();
AUndo does not remove any action, causing infinite undo.
BUndo removes the first action, which is incorrect for stack.
CUndo removes all actions at once, losing history.
DUndo removes the last action, which is correct behavior.
Attempts:
2 left
💡 Hint

Think about how undo should remove the most recent action.

🚀 Application
expert
3:00remaining
Choosing Data Structure for Browser History Navigation

You want to implement browser history navigation with back and forward buttons. Which data structure or combination is best suited?

AUse two stacks: one for back history and one for forward history.
BUse a single stack to store all visited pages.
CUse a queue to store visited pages in order.
DUse a linked list with no stacks or queues.
Attempts:
2 left
💡 Hint

Consider how back and forward buttons move through history in opposite directions.

Practice

(1/5)
1. Which data structure should you use when you need to process tasks in the exact order they arrive, like customers waiting in line at a store?
easy
A. Stack
B. Queue
C. Array
D. Tree

Solution

  1. Step 1: Understand the order requirement

    Tasks must be processed in the order they arrive, which means first-in, first-out (FIFO).
  2. Step 2: Match the data structure to FIFO

    A queue follows FIFO, so it fits the requirement perfectly.
  3. Final Answer:

    Queue -> Option B
  4. Quick Check:

    FIFO = Queue [OK]
Hint: Use queue for first-in, first-out order [OK]
Common Mistakes:
  • Confusing stack (LIFO) with queue (FIFO)
  • Choosing array without order guarantees
  • Thinking tree fits linear order
2. Which of the following is the correct TypeScript syntax to add an element to the top of a stack implemented as an array named stack?
easy
A. stack.unshift(element);
B. stack.pop(element);
C. stack.shift(element);
D. stack.push(element);

Solution

  1. Step 1: Identify stack operation to add element

    Adding to the top of a stack means pushing an element onto it.
  2. Step 2: Match with correct array method

    In TypeScript, push() adds an element to the end (top) of the array acting as a stack.
  3. Final Answer:

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

    Push adds to stack top [OK]
Hint: Use push() to add on stack top [OK]
Common Mistakes:
  • Using pop() which removes instead of adds
  • Using shift() or unshift() which affect queue ends
  • Confusing push() with unshift()
3. What will be the output of the following TypeScript code?
const queue: number[] = [];
queue.push(1);
queue.push(2);
queue.push(3);
const first = queue.shift();
console.log(queue);
medium
A. [3, 2]
B. [1, 2, 3]
C. [2, 3]
D. []

Solution

  1. Step 1: Trace queue operations

    Elements 1, 2, 3 are added to the queue using push(). Then shift() removes the first element (1).
  2. Step 2: Determine queue state after removal

    After removing 1, the queue contains [2, 3].
  3. Final Answer:

    [2, 3] -> Option C
  4. Quick Check:

    shift() removes first, queue left with [2, 3] [OK]
Hint: shift() removes front in queue [OK]
Common Mistakes:
  • Thinking push adds to front instead of back
  • Confusing shift() with pop()
  • Expecting stack order instead of queue order
4. The following TypeScript code is intended to implement a stack but has a bug. What is the bug?
const stack: number[] = [];
stack.push(10);
stack.push(20);
const top = stack.shift();
console.log(top);
medium
A. Using shift() instead of pop() to remove top element
B. Using push() instead of unshift() to add element
C. Not initializing the stack array
D. Using console.log incorrectly

Solution

  1. Step 1: Identify stack removal method

    Stack removes the top element using pop(), not shift().
  2. Step 2: Analyze code's removal method

    The code uses shift(), which removes the first element, breaking stack behavior.
  3. Final Answer:

    Using shift() instead of pop() to remove top element -> Option A
  4. Quick Check:

    Stack removal = pop(), not shift() [OK]
Hint: Stack removes top with pop(), not shift() [OK]
Common Mistakes:
  • Confusing shift() with pop()
  • Thinking push() adds to front
  • Ignoring array initialization
5. You are designing a system to undo and redo user actions. Which data structure combination is best suited for this, and why?
hard
A. Two stacks: one for undo, one for redo, because stacks allow reversing actions easily
B. Two queues: one for undo, one for redo, because queues maintain order of actions
C. One stack only, because redo is not needed
D. One queue only, because undo and redo are processed in order

Solution

  1. Step 1: Understand undo/redo requirements

    Undo and redo require reversing and reapplying actions in last-in, first-out order.
  2. Step 2: Match data structures to behavior

    Stacks naturally support LIFO, so using two stacks (undo and redo) allows easy push/pop for these operations.
  3. Final Answer:

    Two stacks: one for undo, one for redo, because stacks allow reversing actions easily -> Option A
  4. Quick Check:

    Undo/Redo = Two stacks (LIFO) [OK]
Hint: Undo/Redo needs two stacks for LIFO action reversal [OK]
Common Mistakes:
  • Using queues which do not reverse order easily
  • Ignoring redo functionality
  • Using only one data structure
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