# Amazon DSA Questions with Answers

8 previously-asked dsa questions from Amazon's hiring process, each with the correct answer and a worked explanation. Written against the company's actual test pattern.

_Source: Astra (https://useastra.in). Updated 2026-09-05._

### 1. Given an array and a target, return the indices of the two numbers that add to the target.

**Answer:** Use a hash map from value to index. For each element x, check if target minus x is present; if so return the pair, else store x. Time O(n).

**Explanation:** Two Sum via hash map complement.

### 2. Given heights of vertical lines, find the container that holds the most water.

**Answer:** Use two pointers at the ends. Area is min of the two heights times width; record the max, then move the pointer at the shorter line inward. Time O(n).

**Explanation:** Two-pointer, move the shorter side.

### 3. Merge two sorted linked lists into one sorted list.

**Answer:** Use a dummy head and compare the heads of both lists, appending the smaller and advancing that list. Attach the remainder at the end. Time O(m + n).

**Explanation:** Dummy node merge like merge sort.

### 4. Find the lowest common ancestor of two nodes in a binary search tree.

**Answer:** Walk from the root: if both values are smaller go left, if both larger go right, otherwise the current node is the split point and thus the LCA. Time O(h).

**Explanation:** Use BST ordering to find the split node.

### 5. Detect whether a linked list has a cycle.

**Answer:** Use Floyd slow and fast pointers; if they meet there is a cycle, if fast reaches null there is none. Time O(n), space O(1).

**Explanation:** Floyd tortoise and hare.

### 6. Reverse a singly linked list.

**Answer:** Iterate with prev, curr, and next pointers, reversing each link, until curr is null; prev is the new head. Time O(n), space O(1).

**Explanation:** Iterative pointer reversal.

### 7. Perform a level order traversal of a binary tree.

**Answer:** Use a queue starting with the root. Repeatedly dequeue a node, record it, and enqueue its children, processing level by level. Time O(n).

**Explanation:** BFS with a queue.

### 8. Count the number of islands in a grid of land and water.

**Answer:** Scan the grid; on each unvisited land cell run DFS or BFS to flood-fill the connected region and increment the count. Time O(rows * cols).

**Explanation:** Flood fill connected land.

More Amazon preparation, including the full recruitment process: https://useastra.in/campus/amazon
