📖 Scenario: You are managing a line of customers waiting for service at a store. You want to check who is at the front of the line without removing them.
🎯 Goal: Build a simple queue using an array and write code to peek at the front element of the queue.
📋 What You'll Learn
Create a queue as an array with exact customer names
Create a variable to hold the front element
Write code to peek (access) the front element without removing it
Print the front element
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting for service or tasks waiting to be processed.
💼 Career
Understanding queues and how to peek at elements is important for programming tasks like scheduling, buffering, and managing resources efficiently.
Progress0 / 4 steps
1
Create the queue array
Create an array called queue with these exact strings in order: 'Alice', 'Bob', 'Charlie', 'Diana'.
DSA Typescript
Hint
Use square brackets [] to create an array and separate names with commas.
2
Create a variable for the front element
Create a variable called frontElement and set it to null initially.
DSA Typescript
Hint
Use let to create a variable that can change later.
3
Peek the front element of the queue
Set frontElement to the first element of the queue array without removing it.
DSA Typescript
Hint
Use queue[0] to access the first item in the array.
4
Print the front element
Print the value of frontElement using console.log.
DSA Typescript
Hint
Use console.log(frontElement); to show the front element in the console.
Practice
(1/5)
1. What does the peek operation do in a queue?
easy
A. Returns the front element without removing it
B. Removes the front element from the queue
C. Adds an element to the back of the queue
D. Checks if the queue is empty
Solution
Step 1: Understand the peek operation
The peek operation looks at the front element of the queue but does not remove it.
Step 2: Compare with other operations
Removing is done by dequeue, adding by enqueue, and checking empty is a separate check.
Final Answer:
Returns the front element without removing it -> Option A
Quick Check:
Peek = front element without removal [OK]
Hint: Peek shows front without removal [OK]
Common Mistakes:
Confusing peek with dequeue
Thinking peek adds elements
Assuming peek checks emptiness
2. Which of the following is the correct TypeScript syntax to peek the front element of a queue implemented as an array named queue?
easy
A. const front = queue.pop();
B. const front = queue.shift();
C. const front = queue.push();
D. const front = queue[0];
Solution
Step 1: Understand array methods for queue
In a queue using an array, the front element is at index 0.
Step 2: Identify correct syntax for peek
Accessing queue[0] returns the front without removing it. pop() removes from end, shift() removes from front, push() adds to end.
Final Answer:
const front = queue[0]; -> Option D
Quick Check:
Peek = queue[0] [OK]
Hint: Peek front is queue[0], not pop or shift [OK]
Common Mistakes:
Using pop() which removes last element
Using shift() which removes front element
Using push() which adds element
3. What will be the output of the following TypeScript code?
A. pop() removes last element, should use items[0]
B. pop() is correct for peek operation
C. items array should be private
D. peek method should return void
Solution
Step 1: Understand pop() behavior
pop() removes and returns the last element, not the front.
Step 2: Correct way to peek front
To peek front, return this.items[0] without removing it.
Final Answer:
pop() removes last element, should use items[0] -> Option A
Quick Check:
pop() ≠ peek front [OK]
Hint: pop() removes last, peek front is items[0] [OK]
Common Mistakes:
Using pop() for peek
Not returning front element correctly
Ignoring array indexing
5. You have a queue implemented as a linked list with nodes having value and next. How do you peek the front element safely when the queue might be empty?
hard
A. Return head.value directly without checks
B. Check if head is null; if not, return head.value, else return null
C. Traverse to the last node and return its value
D. Remove the head node and return its value
Solution
Step 1: Understand linked list queue front
The front element is at the head node of the linked list.
Step 2: Handle empty queue case
If head is null, queue is empty, so return null or undefined safely.
Step 3: Return front value safely
If head exists, return head.value without removing the node.
Final Answer:
Check if head is null; if not, return head.value, else return null -> Option B
Quick Check:
Peek front safely = check head then return value [OK]
Hint: Check head null before peek to avoid errors [OK]
Common Mistakes:
Not checking for empty queue
Returning value without null check
Removing node instead of peeking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.