📖 Scenario: You are managing a queue of customers waiting in line at a store. The queue is represented as an array where the first customer is at the beginning of the array.When a customer is served, they leave the queue from the beginning.
🎯 Goal: Build a program that removes the first customer from the queue (array) and shows the updated queue.
📋 What You'll Learn
Create an array called queue with these exact customer names: "Alice", "Bob", "Charlie", "Diana", "Ethan"
Create a variable called servedCustomer to hold the name of the customer who is served (removed from the beginning)
Remove the first customer from the queue and assign their name to servedCustomer
Print the name of the served customer and the updated queue in the format: "Served: Alice" and "Queue: [Bob Charlie Diana Ethan]"
💡 Why This Matters
🌍 Real World
Queues are common in real life, like lines at stores or tasks waiting to be processed. Managing them with arrays helps in many programs.
💼 Career
Understanding how to remove elements from arrays is important for jobs involving data processing, backend development, and system design.
Progress0 / 4 steps
1
Create the initial queue array
Create an array called queue with these exact customer names: "Alice", "Bob", "Charlie", "Diana", "Ethan"
DSA Go
Hint
Use a slice of strings and assign the exact names in order.
2
Create a variable to hold the served customer
Create a variable called servedCustomer of type string to hold the name of the customer who will be served
DSA Go
Hint
Declare servedCustomer as a string variable without assigning a value yet.
3
Remove the first customer and assign to servedCustomer
Remove the first customer from the queue and assign their name to servedCustomer. Update queue to remove that first customer.
DSA Go
Hint
Assign the first element of queue to servedCustomer. Then slice queue from index 1 to remove the first customer.
4
Print the served customer and updated queue
Print the name of the served customer using fmt.Println("Served:", servedCustomer) and then print the updated queue using fmt.Println("Queue:", queue)
DSA Go
Hint
Use fmt.Println twice: once for the served customer and once for the updated queue.
Practice
(1/5)
1. What happens to the array [10, 20, 30, 40] after deleting the first element at the beginning?
easy
A. [40]
B. [10, 20, 30]
C. [20, 30, 40]
D. [30, 40]
Solution
Step 1: Understand deletion at beginning
Deleting the first element removes the element at index 0, which is 10.
Step 2: Shift remaining elements left
The remaining elements 20, 30, 40 shift left to fill the gap, resulting in [20, 30, 40].
Final Answer:
[20, 30, 40] -> Option C
Quick Check:
Remove first element = [20, 30, 40] [OK]
Hint: Remove first element, shift left, rest remain [OK]
Common Mistakes:
Removing last element instead of first
Not shifting elements left after deletion
Leaving the first element in place
2. Which of the following Go code snippets correctly deletes the first element from a slice arr?
easy
A. arr = append(arr, 0)
B. arr = arr[1:]
C. arr = arr[:len(arr)-1]
D. arr = arr[0:]
Solution
Step 1: Understand slice slicing syntax
In Go, arr[1:] creates a slice from index 1 to end, effectively removing the first element.
Step 2: Check other options
arr[:len(arr)-1] removes last element, append(arr, 0) adds element, arr[0:] returns full slice.
Final Answer:
arr = arr[1:] -> Option B
Quick Check:
Slice from index 1 removes first element [OK]
Hint: Use arr = arr[1:] to drop first element [OK]