← 返回博客列表
Amazon

Amazon OA Real Questions: Max Interval Selection + Ball Partition Min Moves (n log n)

2026-03-29

Two Amazon OA problems testing binary search + greedy + prefix sums in combination. The ideas aren't hard but the implementation details matter — stay methodical. Full walkthrough below.

Amazon OA Q1 Screenshot


Exam Overview

Item Details
Platform HackerRank
Questions 2
Difficulty Medium / Medium-Hard
Role SDE
Core Topics Intervals, Binary Search, Prefix Sum, Greedy

Q1: Max Interval Selection (Central Overlap)

Amazon OA Q1

Core Idea

Given n intervals, select as many as possible such that there exists one "center interval" that intersects every other selected interval. Find the maximum number of intervals you can select.

Solution Approach

Brute Force (O(n²) — TLE)

Enumerate each interval as the center, count how many others it intersects, and take the maximum. With n up to 10⁵, this is too slow.

Optimized: Reverse Counting + Binary Search (O(n log n))

Key insight: Instead of counting intervals that do intersect the center, count intervals that definitely don't intersect — this is easy to do with binary search.

Two intervals [a, b] and [c, d] are non-overlapping if and only if:

Algorithm:

  1. Sort all intervals by left endpoint; also build an array sorted by right endpoint.
  2. For each interval [a, b] as the center:
    • Binary search the right-endpoint array: count intervals with right < a (entirely left → non-overlapping)
    • Binary search the left-endpoint array: count intervals with left > b (entirely right → non-overlapping)
    • Non-overlapping count = sum of both
    • Overlapping count = n − non-overlapping count
  3. Return the maximum overlapping count across all candidate centers.

Solution Template

import bisect

def solution(intervals):
    n = len(intervals)
    if n == 0:
        return 0

    left_sorted  = sorted(x[0] for x in intervals)
    right_sorted = sorted(x[1] for x in intervals)

    ans = 0
    for a, b in intervals:
        # Intervals with right endpoint < a: entirely to the left
        left_non_overlap  = bisect.bisect_left(right_sorted, a)
        # Intervals with left endpoint > b: entirely to the right
        right_non_overlap = n - bisect.bisect_right(left_sorted, b)

        ans = max(ans, n - left_non_overlap - right_non_overlap)

    return ans

Complexity

Key Insight

Counting "intersecting intervals" directly requires enumeration. Counting "non-overlapping intervals" can be done in O(log n) per query via binary search — this is the core optimization.


Q2: Ball Partition Min Moves

Amazon OA Q2

Core Idea

Balls numbered 1 to n are distributed across three groups A, B, C. You can move any ball to a different group. The goal: after all moves, concatenating A + B + C must equal 1, 2, 3, ..., n in order. Find the minimum number of moves.

Solution Approach

Key Observation

The final arrangement A + B + C = [1..n] means the groups must look like:

for some 0 ≤ i ≤ j ≤ n (empty groups allowed).

Core Property

The relative order within each group doesn't matter — only whether each ball is in the correct group. Balls already in the right group need zero moves; all others need exactly one move.

Minimum moves = n − (number of balls already in the correct group)

Algorithm

  1. Record which group each ball currently belongs to.
  2. Build prefix sum arrays for each group:
    • prefix_a[k] = count of balls in {1..k} currently in A
    • prefix_b[k] = count of balls in {1..k} currently in B
    • prefix_c[k] = count of balls in {1..k} currently in C
  3. Enumerate split points i and j; for each pair compute correctly-placed balls in O(1).
  4. Return n − max(correctly placed).

Solution Template

def solution(A, B, C):
    n = len(A) + len(B) + len(C)

    group = {}
    for ball in A: group[ball] = 0
    for ball in B: group[ball] = 1
    for ball in C: group[ball] = 2

    prefix_a = [0] * (n + 1)
    prefix_b = [0] * (n + 1)
    prefix_c = [0] * (n + 1)
    for k in range(1, n + 1):
        prefix_a[k] = prefix_a[k-1] + (1 if group[k] == 0 else 0)
        prefix_b[k] = prefix_b[k-1] + (1 if group[k] == 1 else 0)
        prefix_c[k] = prefix_c[k-1] + (1 if group[k] == 2 else 0)

    min_moves = n
    for i in range(0, n + 1):
        for j in range(i, n + 1):
            correct = (prefix_a[i]
                     + prefix_b[j] - prefix_b[i]
                     + prefix_c[n] - prefix_c[j])
            min_moves = min(min_moves, n - correct)

    return min_moves

Further optimization: The double loop is O(n²); it can be reduced to O(n) by precomputing suffix maximums — the template above illustrates the core logic.

Complexity

Key Insight

Group-internal order is irrelevant — only group membership matters. Enumerating split points with prefix sums turns each "count correctly placed" query into O(1).


Side-by-Side Comparison

Question Core Technique Time Space
Q1 Interval Selection Sort + binary search reverse count O(n log n) O(n)
Q2 Ball Partition Enumerate split + prefix sums O(n) O(n)

Common Mistakes

Question Mistake Correct Approach
Q1 Directly enumerate overlapping intervals O(n²) Count non-overlapping via binary search
Q1 Wrong bisect variant (left vs right) Carefully distinguish strict vs non-strict bounds
Q2 Factoring in intra-group order Order within groups is irrelevant
Q2 Forgetting empty-group edge cases Allow i=0 or j=n in enumeration

Amazon OA Strategy

On-site Pacing

  1. Read both problems first — allocate time based on relative difficulty
  2. Interval problems (Q1 type): Think "when do two intervals NOT overlap?" — reverse counting is usually cleaner
  3. Partition problems (Q2 type): Fix the final structure first, then use prefix sums to speed up counting
  4. Run through the provided examples; verify empty / single-element edge cases

Related LeetCode Practice

# Problem Covers
435 Non-overlapping Intervals Q1 interval non-overlap
452 Minimum Arrows to Burst Balloons Q1 interval overlap
56 Merge Intervals Q1 interval merging
1043 Partition Array for Maximum Sum Q2 split-point enumeration
813 Largest Sum of Averages Q2 prefix sum + enumerate

🚀 Need Amazon OA Assistance?

oavoservice provides stable, long-term real-time OA/VO support for Google, Amazon, TikTok, Uber, Bloomberg, Microsoft and more — both domestic and overseas interviews covered. High question-bank coverage.

Add WeChat: Coding0201

What you get:

📱 WeChat: Coding0201 | 💬 Telegram: @OAVOProxy | 📧 [email protected]


Contact

Email: [email protected]
Telegram: @OAVOProxy