← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jul 2026

Summary

Amazon SWE online assessment with a ring-distance problem that looks straightforward until you realize they expect O(1) per query, not a fresh traversal each time. Prep-heavy problem with a clean prefix-sum trick once you see it.

Questions Asked (1)

Q1

Given n stops arranged in a ring with weighted edges between consecutive stops, and a stream of (start, end) query pairs, return the sum of shortest arc distances across all queries. Optimize for repeated queries against the same ring.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The problem is dressed up in some delivery or sightseeing story so it takes a minute to see it's just a circular distance thing.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Preprocess the ring by computing prefix sums of edge weights to enable O(1) distance queries between any two stops. For each query, compute the clockwise distance and subtract from the total circumference to get the counterclockwise distance, then take the minimum. Sum these minimum distances across all queries.

Pro tip: Mention that the prefix sum array should be built once and reused for all queries, and that the total circumference is simply the last element of the prefix sum array. This shows you understand the importance of preprocessing for repeated queries.

1. Understand the problem and clarify assumptions

Confirm that the ring is directed or undirected? Typically, edges have weights and you can travel both ways. Clarify that queries are (start, end) and you need the shortest arc distance. Also confirm if start and end are indices or stop identifiers.

2. Preprocess the ring for O(1) distance queries

Compute prefix sums of edge weights along the ring. Let total be the sum of all edge weights. For any two stops i and j, the clockwise distance from i to j is (prefix[j] - prefix[i] + total) % total if indices are 0-based and prefix[0]=0. The counterclockwise distance is total minus that.

3. Answer each query in O(1) time

For each query (start, end), compute the clockwise distance and the counterclockwise distance, then take the minimum. Add this minimum to the running sum.

4. Analyze time and space complexity

Preprocessing takes O(n) time and O(n) space. Each query is answered in O(1) time, so total time for q queries is O(n + q). This is optimal for repeated queries.

5. Discuss trade-offs and edge cases

Consider if the ring is very large and memory is a concern: you could use a segment tree or Fenwick tree for dynamic updates, but for static weights, prefix sums are best. Handle edge cases like start == end (distance 0) and ensure modulo arithmetic works correctly.

Key Points to Mention

  • Prefix sums for O(1) distance queries
  • Total circumference and modulo arithmetic
  • Clockwise vs counterclockwise distance
  • Time complexity: O(n) preprocessing, O(1) per query
  • Space complexity: O(n) for prefix sums
  • Handling edge cases: start == end, large weights, negative? (weights are positive)

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