📖 Scenario: You are managing a line of customers waiting to buy tickets at a counter. You want to check who is the first customer in line without removing them.
🎯 Goal: Build a simple program that creates a queue of customers, then shows the name of the first customer waiting in line.
📋 What You'll Learn
Create a queue with exact customer names in order
Use a variable to store the front element of the queue
Print the front element without removing it
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting, print jobs, or tasks waiting to be processed.
💼 Career
Understanding queues and how to peek at elements is important for software developers working with task scheduling, event handling, and data streaming.
Progress0 / 4 steps
1
Create a queue with customers
Create a std::queue<std::string> called customers and add these names in order: "Alice", "Bob", "Charlie".
DSA C++
Hint
Use customers.push("Name") to add each customer in order.
2
Store the front customer
Create a std::string variable called frontCustomer and set it to the front element of the customers queue using customers.front().
DSA C++
Hint
Use customers.front() to get the first element without removing it.
3
Print the front customer
Use std::cout to print the text "First customer in line: " followed by the value of frontCustomer.
DSA C++
Hint
Use std::cout with << to print the message and variable.
4
Run and check output
Run the program and ensure it prints exactly: First customer in line: Alice
DSA C++
Hint
Make sure the output matches exactly, including capitalization and spacing.
Practice
(1/5)
1. What does the peek() operation do in a queue?
easy
A. Removes the front element from the queue
B. Returns the front element without removing it
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 without changing the queue.
Step 2: Compare with other operations
Unlike dequeue, peek does not remove the element; it only returns it.
Final Answer:
Returns the front element without removing it -> Option B
Quick Check:
peek() = front element [OK]
Hint: Peek shows front without removal [OK]
Common Mistakes:
Confusing peek with dequeue
Thinking peek adds elements
Assuming peek empties queue
2. Which of the following is the correct syntax to peek the front element of a std::queue<int> named q in C++?
easy
A. q.front();
B. q.peek();
C. q.top();
D. q.getFront();
Solution
Step 1: Recall C++ queue methods
In C++, std::queue uses front() to access the front element.
Step 2: Check other options
peek() and getFront() are not valid methods; top() is for stacks, not queues.
Final Answer:
q.front(); -> Option A
Quick Check:
std::queue front() method = q.front() [OK]
Hint: Use front() to peek in std::queue [OK]
Common Mistakes:
Using peek() which doesn't exist
Using top() which is for stacks
Trying getFront() which is invalid
3. What will be the output of the following C++ code?
The code uses q.peek(), but std::queue does not have a peek() method.
Step 2: Identify correct method
The correct method to access the front element is q.front().
Final Answer:
Using q.peek() which is not a valid method -> Option C
Quick Check:
std::queue peek method = front() [OK]
Hint: Use front(), not peek(), to access front element [OK]
Common Mistakes:
Using peek() method
Assuming peek() exists
Ignoring compiler errors
5. Given a queue q containing integers, write a C++ function int peekFrontOrDefault(std::queue<int>& q, int defaultVal) that returns the front element if the queue is not empty, or defaultVal if it is empty. What is the correct implementation?
hard
A. return q.front();
B. if (q.empty()) return q.front(); else return defaultVal;
C. if (q.size() > 1) return q.front(); else return defaultVal;
D. if (!q.empty()) return q.front(); else return defaultVal;
Solution
Step 1: Check if queue is empty
Use q.empty() to verify if queue has elements.
Step 2: Return front or default value
If not empty, return q.front(); otherwise, return defaultVal.
Final Answer:
if (!q.empty()) return q.front(); else return defaultVal; -> Option D
Quick Check:
Check empty before front() to avoid errors [OK]
Hint: Always check empty before front() to avoid errors [OK]
Common Mistakes:
Not checking empty before front()
Wrong condition on size
Returning front when empty
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.