Guest Queue with ArrayList
An event desk has a list of confirmed guests and a list of walk-in guests who arrived just before the doors open. Merge them into one final queue that preserves the original confirmed order first, then appends all walk-in guests after the confirmed list.
For example, if confirmed = ["Ayu", "Bima"] and walkIns = ["Cici", "Dodi"], the result should be ["Ayu", "Bima", "Cici", "Dodi"]. If the walk-in list is empty, the result is the confirmed list unchanged. If both are empty, the result is an empty list.
This is a simple queue merge operation where order matters: all confirmed guests come before any walk-in guests. No sorting or de-duplication is needed. The operation preserves the original sequence within each group.
The solution uses ArrayList's copy constructor to create a new list from the confirmed list, then addAll() to append all walk-in guests at the end. This avoids mutating the original input lists and runs in O(n + m) time.
Edge cases include both lists empty (return empty list), one list empty (return the other), and names with special characters or spaces (they are treated as plain strings).
Example Input & Output
If no guests were confirmed earlier, the walk-ins become the whole queue.
The walk-in names are appended after the confirmed list.
No walk-ins means the original order stays unchanged.
Algorithm Flow
Solution Approach
Create a new ArrayList from the confirmed list and append all walk-in guests using addAll().
The ArrayList copy constructor takes an existing collection and copies all its elements into a new list, preserving insertion order. Then addAll() appends every element from the walk-in list to the end of the result list. This produces the correct order: all confirmed guests followed by all walk-in guests.
An alternative manual approach uses a loop with add():Both approaches achieve the same O(n + m) time complexity and O(n + m) space complexity for the result list.
Best Answers
import java.util.List;
import java.util.ArrayList;
class Solution {
public static List<String> mergeGuestQueue(List<String> confirmed, List<String> walkIns) {
List<String> result = new ArrayList<>();
for (String name : confirmed) {
result.add(name);
}
for (String name : walkIns) {
result.add(name);
}
return result;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
