← Amazon Interview Insights

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

Intermediate
Jun 2026

Summary

Amazon OA for a SWE role. One algorithmic problem, cycle graph shortest path with multiple queries. Pretty standard competitive programming territory but the constraint sizes mean you can't brute force it.

Questions Asked (1)

Q1

You have a circular array of N nodes where each element represents the clockwise distance to the next node. Given Q queries each with two nodes, find the shortest path between them along the cycle (clockwise or counterclockwise) and return the total sum across all queries.

Algorithms & Data Structures
Author's notes

The core idea isn't hard once you see it: precompute prefix sums of the distances, then for any pair the clockwise distance is just a prefix difference, and counterclockwise is total minus that.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem constraints and edge cases, then propose an efficient solution using prefix sums to answer each query in O(1) time. Explain how to compute the clockwise and counterclockwise distances and take the minimum, summing across all queries.

Pro tip: Mention that the total sum of all distances is constant, so the counterclockwise distance is totalSum minus clockwise distance, avoiding a second traversal. Also, discuss handling large N and Q with 64-bit integers to prevent overflow.

1. Clarify the problem

Ask about constraints (N, Q, distance ranges), whether nodes are 0-indexed or 1-indexed, and if the array is given as distances between consecutive nodes. Confirm that queries are independent and we need the sum of shortest paths.

2. Preprocess for O(1) queries

Build a prefix sum array where prefix[i] is the sum of distances from node 0 to node i clockwise. Compute totalSum as the sum of all distances. This allows O(1) clockwise distance between any two nodes.

3. Answer each query

For nodes u and v, compute clockwise distance as (prefix[v] - prefix[u] + totalSum) % totalSum. The counterclockwise distance is totalSum minus clockwise distance. The shortest path is the minimum of the two.

4. Sum and return

Accumulate the shortest distances for all queries into a result variable. Use a 64-bit integer to avoid overflow. Return the total sum.

5. Analyze complexity

Time: O(N + Q) for preprocessing and answering queries. Space: O(N) for prefix sums. This is optimal for the given constraints.

Key Points to Mention

  • Prefix sum array for O(1) distance queries
  • Total sum of all distances is constant; counterclockwise distance = totalSum - clockwise distance
  • Handling circular indexing with modulo arithmetic
  • Using 64-bit integers to prevent overflow when summing large distances
  • Edge cases: u == v, N=1, large N and Q
  • Time and space complexity analysis

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