Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Stack Implementation Using Linked List in DSA Typescript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new node with the given value.

DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = [1];
  }
}
Drag options to blanks, or click blank then click option'
Aundefined
B0
Cthis
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to undefined or 0 instead of null.
2fill in blank
medium

Complete the push method to add a new node at the top of the stack.

DSA Typescript
push(value: number): void {
  const newNode = new Node(value);
  newNode.next = this.top;
  this.top = [1];
  this.size++;
}
Drag options to blanks, or click blank then click option'
AnewNode
Bthis.top
Cthis.size
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning top to value or size instead of the new node.
3fill in blank
hard

Fix the error in the pop method to correctly remove and return the top node's value.

DSA Typescript
pop(): number | null {
  if (!this.top) return null;
  const poppedNode = this.top;
  this.top = poppedNode.next;
  this.size--;
  return [1];
}
Drag options to blanks, or click blank then click option'
ApoppedNode.value
Bthis.top.value
CpoppedNode
Dthis.size
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the node object or the new top's value instead of the popped node's value.
4fill in blank
hard

Fill both blanks to check if the stack is empty and return a boolean.

DSA Typescript
isEmpty(): boolean {
  return this.size [1] 0;
}
Drag options to blanks, or click blank then click option'
A!==
B===
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or !== which give wrong boolean results for emptiness.
5fill in blank
hard

Fill all three blanks to implement peek method that returns the top value without removing it.

DSA Typescript
peek(): number | null {
  if (this.top === [1]) return null;
  return this.top[2][3];
}
Drag options to blanks, or click blank then click option'
Anull
B.
Cvalue
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Returning undefined or accessing value without checking if top is null.

Practice

(1/5)
1. What is the main advantage of implementing a stack using a linked list instead of an array?
easy
A. It allows random access to elements
B. It uses less memory than an array
C. The stack can grow dynamically without a fixed size limit
D. It is faster to access elements by index

Solution

  1. Step 1: Understand stack size limitation in arrays

    Arrays have a fixed size, so stacks implemented with arrays have a maximum capacity.
  2. Step 2: Linked list dynamic size

    Linked lists can grow or shrink dynamically by adding or removing nodes, so stack size is not fixed.
  3. Final Answer:

    The stack can grow dynamically without a fixed size limit -> Option C
  4. Quick Check:

    Linked list stacks grow dynamically = A [OK]
Hint: Linked list stacks grow dynamically, arrays do not [OK]
Common Mistakes:
  • Thinking linked lists use less memory always
  • Confusing stack with queue behavior
  • Assuming random access is possible in linked lists
2. Which of the following is the correct TypeScript syntax to define a node class for a linked list stack?
easy
A. class Node { value: number; next: Node; constructor(value: number) { this.value = value; this.next = null; } }
B. class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } }
C. class Node { value: number; next: null; constructor(value: number) { this.value = value; this.next = null; } }
D. class Node { value: number; next: any; constructor(value: number) { this.value = value; this.next = undefined; } }

Solution

  1. Step 1: Check next pointer type

    In a linked list, next can be another node or null if it's the last node.
  2. Step 2: Verify constructor initialization

    The constructor sets value and initializes next to null.
  3. Final Answer:

    class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } -> Option B
  4. Quick Check:

    next can be Node or null = B [OK]
Hint: next must allow null for last node [OK]
Common Mistakes:
  • Not allowing next to be null
  • Initializing next as undefined
  • Using incorrect type for next
3. Given the following TypeScript code for a stack push operation, what will be the top node's value after pushing 10, then 20?
medium
A. undefined
B. 10
C. null
D. 20

Solution

  1. Step 1: Understand push operation

    Push adds a new node at the top, so the last pushed value becomes the top.
  2. Step 2: Trace pushing 10 then 20

    First push 10: top.value = 10; then push 20: top.value = 20 (new node).
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    Last pushed value is top = 20 [OK]
Hint: Top always holds last pushed value [OK]
Common Mistakes:
  • Assuming bottom value is top
  • Confusing push with pop
  • Not updating top pointer
4. Identify the error in this pop method of a linked list stack implementation:
pop(): number | null {
  if (!this.top) return null;
  const value = this.top.value;
  this.top = this.top.next;
  return this.top.value;
}
medium
A. Returns the new top value instead of popped value
B. Does not check if stack is empty
C. Does not update top pointer
D. Throws error when stack is empty

Solution

  1. Step 1: Analyze return value

    The method returns this.top.value after updating top, which is the new top, not the popped value.
  2. Step 2: Correct return value

    It should return the saved value before updating top to return the popped element.
  3. Final Answer:

    Returns the new top value instead of popped value -> Option A
  4. Quick Check:

    Pop must return popped value, not new top [OK]
Hint: Return saved value before updating top [OK]
Common Mistakes:
  • Returning top after update instead of popped value
  • Not handling empty stack
  • Forgetting to update top pointer
5. You want to implement a method peek() in your linked list stack that returns the top value without removing it. Which is the correct implementation?
hard
A. peek(): number | null { return this.top ? this.top.value : null; }
B. peek(): number | null { const val = this.top.value; this.top = this.top.next; return val; }
C. peek(): number | null { if (!this.top) return null; this.top = this.top.next; return this.top.value; }
D. peek(): number | null { return this.top.next ? this.top.next.value : null; }

Solution

  1. Step 1: Understand peek operation

    Peek returns the top value without changing the stack.
  2. Step 2: Check each option

    peek(): number | null { return this.top ? this.top.value : null; } returns top value if exists, else null, without modifying stack. Others modify top or return wrong node.
  3. Final Answer:

    peek(): number | null { return this.top ? this.top.value : null; } -> Option A
  4. Quick Check:

    Peek returns top value without removal = C [OK]
Hint: Peek returns top value without changing stack [OK]
Common Mistakes:
  • Modifying top pointer in peek
  • Returning next node's value
  • Not handling empty stack
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