← Expedia Interview Insights

Expedia·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Expedia coding screen, one algorithm question about rearranging an array so no two adjacent elements are equal. Pretty standard but the implementation tripped me up more than I expected.

Questions Asked (1)

Q1

Given an integer array, rearrange it so that no two adjacent elements are equal. A valid arrangement is guaranteed to exist. Return any valid result.

Algorithms & Data Structures
Author's notes

The problem sounds straightforward until you actually try to code it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a max-heap to greedily place the most frequent remaining element at each position, ensuring it differs from the previously placed element. This approach guarantees a valid arrangement when one exists and runs in O(n log n) time.

Pro tip: Mention that you can optimize space by using a frequency map and a heap, and that the greedy choice is safe because the problem guarantees a solution. Also, briefly discuss an alternative O(n) approach using the majority element if it exists.

1. Understand the problem and constraints

Clarify that the input is an integer array, a valid arrangement always exists, and we need to return any valid rearrangement. Note that the array can have duplicates.

2. Choose the right data structures

Use a frequency map (hash map) to count occurrences, and a max-heap (priority queue) to efficiently retrieve the most frequent element. This allows O(log n) operations per element.

3. Greedy placement with heap

At each step, pop the most frequent element from the heap. If it's the same as the previously placed element, pop the next most frequent instead. Place the chosen element, decrement its count, and push it back if count > 0.

4. Handle edge cases and termination

Ensure the loop runs until the heap is empty. If at any point the heap is empty but we still need to place an element, the arrangement is impossible (but guaranteed not to happen).

5. Analyze complexity and discuss alternatives

State that time complexity is O(n log n) due to heap operations, and space is O(n) for the frequency map and heap. Mention that if the maximum frequency is ≤ (n+1)/2, a solution exists, and an O(n) approach using the majority element is possible.

Key Points to Mention

  • Greedy algorithm with max-heap to always pick the most frequent available element.
  • Use of a frequency map to count occurrences.
  • Ensuring the chosen element differs from the previous one.
  • Time complexity: O(n log n) due to heap operations.
  • Space complexity: O(n) for the frequency map and heap.
  • Condition for existence: max frequency ≤ (n+1)/2, which is guaranteed here.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.