Code Logo

Guest Queue with ArrayList

Published at22 Apr 2026
Collections Framework Easy 16 views
Like0

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

Example 1
Input
confirmed = [], walkIns = ["Raka", "Sinta"]
Output
["Raka", "Sinta"]
Explanation

If no guests were confirmed earlier, the walk-ins become the whole queue.

Example 2
Input
confirmed = ["Ayu", "Bima"], walkIns = ["Cici", "Dodi"]
Output
["Ayu", "Bima", "Cici", "Dodi"]
Explanation

The walk-in names are appended after the confirmed list.

Example 3
Input
confirmed = ["Nina"], walkIns = []
Output
["Nina"]
Explanation

No walk-ins means the original order stays unchanged.

Algorithm Flow

Recommendation Algorithm Flow for Guest Queue with ArrayList

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

java - Approach 1
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;
    }
}