Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the queue.
DSA Typescript
enqueue(item: number): void {
if (this.rear === this.size - 1) {
console.log('Queue is full');
} else {
this.rear = this.rear [1] 1;
this.queue[this.rear] = item;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' to move rear forward.
Using '*' or '/' which are not valid for index increment.
✗ Incorrect
To add an element, we increase the rear index by 1 using the '+' operator.
2fill in blank
mediumComplete the code to remove an element from the queue.
DSA Typescript
dequeue(): number | null {
if (this.front > this.rear) {
console.log('Queue is empty');
return null;
} else {
const item = this.queue[this.front];
this.front = this.front [1] 1;
return item;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' which moves front backward incorrectly.
Using multiplication or division which are invalid here.
✗ Incorrect
To remove an element, we move the front pointer forward by adding 1.
3fill in blank
hardFix the error in the method that checks if the queue is empty.
DSA Typescript
isEmpty(): boolean {
return this.front [1] this.rear;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which only checks equality, not emptiness.
Using '<' or '!=' which do not correctly check empty condition.
✗ Incorrect
The queue is empty when front is greater than rear, so use '>' operator.
4fill in blank
hardFill both blanks to correctly initialize the queue in the constructor.
DSA Typescript
constructor(size: number) {
this.size = size;
this.queue = new Array(size).fill(0);
this.front = [1];
this.rear = [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting rear to 0 which means queue is not empty initially.
Using null which is not a number index.
✗ Incorrect
Front starts at 0 and rear starts at -1 indicating an empty queue.
5fill in blank
hardFill all three blanks to implement the peek method that returns the front element without removing it.
DSA Typescript
peek(): number | null {
if (this.front [1] this.rear) {
return this.queue[[2]];
} else {
return [3];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' which reverses the empty check.
Returning rear index instead of front element.
Returning 0 instead of null when empty.
✗ Incorrect
Peek returns the front element if front <= rear, else returns null if empty.
