Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the end of the queue.
DSA Javascript
queue.push([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes from the end instead of adding.
Using shift() which removes from the front.
Using unshift() which adds to the front.
✗ Incorrect
To add an element to the end of the queue, use push(element).
2fill in blank
mediumComplete the code to remove the first element from the queue.
DSA Javascript
const first = queue.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes from the end.
Using push() which adds to the end.
Using unshift() which adds to the front.
✗ Incorrect
shift() removes and returns the first element, following FIFO.
3fill in blank
hardFix the error in the code to correctly implement FIFO removal.
DSA Javascript
let removed = queue.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes the last element, breaking FIFO.
Using push() or unshift() which add elements instead of removing.
✗ Incorrect
Using shift() removes the first element, following FIFO correctly.
4fill in blank
hardFill both blanks to create a function that enqueues and dequeues correctly.
DSA Javascript
function processQueue(queue, element) {
queue.[1](element);
return queue.[2]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop to remove which breaks FIFO.
Using unshift to add which adds to front, not end.
✗ Incorrect
push adds element to the end; shift removes from the front, following FIFO.
5fill in blank
hardFill all three blanks to implement a queue with enqueue, dequeue, and peek methods.
DSA Javascript
class Queue { constructor() { this.items = []; } enqueue(element) { this.items.[1](element); } dequeue() { return this.items.[2](); } peek() { return this.items[[3]]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop in dequeue which removes from the end.
Using unshift in enqueue which adds to the front.
Using wrong index in peek.
✗ Incorrect
enqueue uses push to add at end, dequeue uses shift to remove from front, peek returns first element at index 0.
