Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Dequeue Operation in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dequeue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after multiple enqueue and dequeue operations
What is the state of the dequeue after executing the following operations in order?
DSA Typescript
class Dequeue {
  items: number[] = [];

  enqueueFront(element: number) {
    this.items.unshift(element);
  }

  enqueueRear(element: number) {
    this.items.push(element);
  }

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

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

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

const dq = new Dequeue();
dq.enqueueRear(10);
dq.enqueueRear(20);
dq.enqueueFront(5);
dq.dequeueRear();
dq.enqueueFront(3);
dq.dequeueFront();
console.log(dq.print());
A5 -> 10 -> null
B3 -> 5 -> 10 -> null
C5 -> 10 -> 20 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Trace each operation step-by-step, updating the dequeue contents.
❓ Predict Output
intermediate
1:00remaining
Result after dequeueFront on empty dequeue
What will be the output of the following code snippet?
DSA Typescript
class Dequeue {
  items: number[] = [];

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

const dq = new Dequeue();
const result = dq.dequeueFront();
console.log(result);
A0
Bnull
Cundefined
DThrows an error
Attempts:
2 left
💡 Hint
What does shift() return when called on an empty array?
❓ Predict Output
advanced
2:00remaining
Output after complex dequeue operations
What is the printed output after running this code?
DSA Typescript
class Dequeue {
  items: number[] = [];

  enqueueFront(element: number) {
    this.items.unshift(element);
  }

  enqueueRear(element: number) {
    this.items.push(element);
  }

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

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

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

const dq = new Dequeue();
dq.enqueueRear(1);
dq.enqueueRear(2);
dq.enqueueFront(3);
dq.enqueueRear(4);
dq.dequeueFront();
dq.dequeueRear();
dq.enqueueFront(5);
console.log(dq.print());
A5 -> 3 -> 1 -> 2 -> null
B5 -> 1 -> 2 -> null
C1 -> 2 -> null
D3 -> 1 -> 2 -> 4 -> null
Attempts:
2 left
💡 Hint
Follow each enqueue and dequeue carefully, updating the list.
🧠 Conceptual
advanced
1:30remaining
Understanding time complexity of dequeue operations
Which statement correctly describes the time complexity of enqueueFront and dequeueRear operations in a dequeue implemented with a doubly linked list?
AenqueueFront runs in O(1) time, dequeueRear runs in O(n) time because rear removal requires traversal.
BenqueueFront runs in O(n) time, dequeueRear runs in O(1) time because only rear removal is direct.
CBoth enqueueFront and dequeueRear run in O(n) time because traversal is needed to find ends.
DBoth enqueueFront and dequeueRear run in O(1) time because nodes can be added or removed directly at the ends.
Attempts:
2 left
💡 Hint
Think about how doubly linked lists store references to both ends.
🔧 Debug
expert
2:30remaining
Identify the bug causing incorrect dequeueRear behavior
Consider this dequeue implementation. Which option correctly identifies the bug causing dequeueRear to remove from the front instead of the rear?
DSA Typescript
class Dequeue {
  items: number[] = [];

  enqueueRear(element: number) {
    this.items.push(element);
  }

  dequeueRear(): number | undefined {
    if (this.items.length === 0) {
      return undefined;
    }
    return this.items.shift();
  }
}

const dq = new Dequeue();
dq.enqueueRear(10);
dq.enqueueRear(20);
console.log(dq.dequeueRear());
AdequeueRear incorrectly uses shift() instead of pop(), removing from front instead of rear.
BdequeueRear does not check if items is empty before removing, causing runtime error.
CThe items array is not initialized properly, causing dequeueRear to always return undefined.
DenqueueRear uses push() which adds to front, causing dequeueRear to fail.
Attempts:
2 left
💡 Hint
Check which method removes from the front and which from the rear in arrays.

Practice

(1/5)
1. What is the main difference between a dequeue and a queue?
easy
A. A dequeue allows insertion and deletion from both ends.
B. A dequeue only allows insertion at the rear.
C. A dequeue only allows deletion from the front.
D. A dequeue is a fixed size data structure.

Solution

  1. Step 1: Understand queue operations

    A queue allows insertion at the rear and deletion from the front only.
  2. Step 2: Understand dequeue operations

    A dequeue allows insertion and deletion at both front and rear ends.
  3. Final Answer:

    A dequeue allows insertion and deletion from both ends. -> Option A
  4. Quick Check:

    Dequeue = Both ends operations [OK]
Hint: Dequeue = Double-ended queue, both ends allowed [OK]
Common Mistakes:
  • Thinking dequeue is same as queue
  • Assuming insertion only at rear
  • Confusing dequeue with stack
2. Which of the following is the correct TypeScript method to add an element at the front of a dequeue implemented as an array?
easy
A. dequeue.pushFront(element);
B. dequeue.unshift(element);
C. dequeue.addFirst(element);
D. dequeue.insertFront(element);

Solution

  1. Step 1: Recall array methods for front insertion

    In TypeScript/JavaScript, unshift() adds element at the front of an array.
  2. Step 2: Check other options

    Methods like pushFront, addFirst, insertFront do not exist in standard arrays.
  3. Final Answer:

    dequeue.unshift(element); -> Option B
  4. Quick Check:

    Array front insert = unshift() [OK]
Hint: Use unshift() to add at front in arrays [OK]
Common Mistakes:
  • Using push() instead of unshift()
  • Assuming pushFront() exists
  • Confusing with linked list methods
3. Consider this TypeScript code using an array as a dequeue:
let dequeue = [10, 20, 30];
dequeue.pop();
dequeue.unshift(5);
console.log(dequeue);
What is the output?
medium
A. [5, 10, 20]
B. [5, 10, 20, 30]
C. [10, 20, 30, 5]
D. [10, 20]

Solution

  1. Step 1: Execute pop()

    pop() removes last element 30, so array becomes [10, 20].
  2. Step 2: Execute unshift(5)

    unshift(5) adds 5 at front, array becomes [5, 10, 20].
  3. Final Answer:

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

    pop removes last, unshift adds front [OK]
Hint: pop removes last, unshift adds front [OK]
Common Mistakes:
  • Assuming pop removes front
  • Thinking unshift adds at end
  • Confusing array order after operations
4. The following TypeScript code tries to remove an element from the front of a dequeue implemented as an array:
let dequeue = [1, 2, 3];
dequeue.removeFront();
console.log(dequeue);
What is the issue and how to fix it?
medium
A. removeFront() works only on linked lists; convert array to linked list.
B. removeFront() removes last element; use pop() instead.
C. removeFront() is not a valid method; use shift() instead.
D. No issue; code runs correctly.

Solution

  1. Step 1: Identify invalid method

    Arrays in TypeScript do not have removeFront() method; this causes a runtime error.
  2. Step 2: Use correct method for front removal

    shift() removes the first element from an array correctly.
  3. Final Answer:

    removeFront() is not a valid method; use shift() instead. -> Option C
  4. Quick Check:

    Front removal in arrays = shift() [OK]
Hint: Use shift() to remove front element in arrays [OK]
Common Mistakes:
  • Using non-existent removeFront() method
  • Confusing pop() with front removal
  • Thinking arrays have linked list methods
5. You have a dequeue implemented as an array: let dq = [1, 2, 3, 4];. You perform these operations:
dq.pop();
dq.unshift(0);
dq.push(5);
dq.shift();
console.log(dq);
What is the final state of the dequeue?
hard
A. [0, 1, 2, 3, 5]
B. [2, 3, 4, 5]
C. [1, 2, 3, 4, 5]
D. [1, 2, 3, 5]

Solution

  1. Step 1: Execute pop()

    pop() removes last element 4, dq becomes [1, 2, 3].
  2. Step 2: Execute unshift(0)

    unshift(0) adds 0 at front, dq becomes [0, 1, 2, 3].
  3. Step 3: Execute push(5)

    push(5) adds 5 at end, dq becomes [0, 1, 2, 3, 5].
  4. Step 4: Execute shift()

    shift() removes first element 0, dq becomes [1, 2, 3, 5].
  5. Final Answer:

    [1, 2, 3, 5] -> Option D
  6. Quick Check:

    pop removes end, unshift adds front, push adds end, shift removes front [OK]
Hint: Track each operation stepwise on array [OK]
Common Mistakes:
  • Forgetting shift removes front element
  • Mixing order of operations
  • Assuming unshift adds at end
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