← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE coding round with three problems, two of them linked list questions and one heap/greedy problem. Nothing too wild but the mix felt a bit scattered.

Questions Asked (3)

Q1

Given a string, rearrange its characters so that no two adjacent characters are the same. Return an empty string if it's not possible.

Algorithms & Data Structures
Author's notes

This one tripped me up more than it should have.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a greedy approach with a max-heap to always place the most frequent remaining character that is different from the last placed character. If at any point the most frequent character is the only option and matches the last placed, return an empty string. This ensures feasibility and efficiency.

Pro tip: Mention the edge case where the maximum frequency exceeds (n+1)/2, which makes rearrangement impossible. Also, discuss the time complexity O(n log k) where k is the number of distinct characters, and note that using a heap is optimal for this problem.

1. Check Feasibility

Count the frequency of each character. If the maximum frequency is greater than (n+1)/2, return an empty string immediately.

2. Initialize Data Structures

Use a max-heap (priority queue) to store characters by their frequencies. Also, keep track of the last placed character and its remaining count.

3. Greedy Placement

While the heap is not empty, pop the character with the highest frequency. If it is the same as the last placed character, pop the next highest. If no other character is available, return an empty string.

4. Update and Reinsert

Append the chosen character to the result, decrement its frequency, and if it still has remaining count, push it back into the heap after placing the next character (to avoid immediate reuse).

5. Return Result

Once the heap is empty, return the constructed string. If at any point we cannot place a character, return an empty string.

Key Points to Mention

  • Time complexity: O(n log k) where n is the length of the string and k is the number of distinct characters.
  • Space complexity: O(k) for the heap and frequency map.
  • Edge cases: empty string, single character, all characters same, maximum frequency condition.
  • Alternative approaches: sorting and interleaving, but heap is more efficient.
  • Correctness proof: greedy choice ensures no adjacent duplicates and feasibility.
  • Handling ties: any character with same frequency can be chosen, but must avoid last placed.

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

Q2

Merge k sorted linked lists into one sorted linked list.

Algorithms & Data Structures
Author's notes

Classic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (e.g., k and total nodes) and discussing naive approaches like merging lists one by one. Then present an optimal solution using a min-heap or divide-and-conquer, explaining time and space complexity. Finally, walk through a small example to demonstrate correctness.

Pro tip: Amazon values scalability and efficiency, so emphasize the O(N log k) time complexity of the heap approach and discuss how it handles large inputs. Also, mention edge cases like empty lists and duplicate values to show thoroughness.

1. Clarify requirements and constraints

Ask about the number of lists (k), total number of nodes (N), and whether the lists are sorted in ascending order. Confirm if you can modify the input lists or need to create a new one.

2. Discuss naive approaches

Mention the simple approach of merging lists sequentially, which takes O(kN) time, and explain why it's inefficient for large k.

3. Propose optimal solution

Present the min-heap approach: insert the head of each list into a min-heap, then repeatedly extract the minimum and add the next node from that list. This yields O(N log k) time and O(k) space.

4. Analyze complexity and edge cases

State the time and space complexity clearly. Discuss edge cases: empty input, lists of different lengths, and duplicate values.

5. Walk through an example

Trace the algorithm on a small example (e.g., k=3 lists) to demonstrate how the heap maintains the sorted order and how the result is built.

Key Points to Mention

  • Time complexity: O(N log k) where N is total nodes and k is number of lists
  • Space complexity: O(k) for the heap (or O(1) extra if using divide-and-conquer with recursion stack O(log k))
  • Min-heap (priority queue) to efficiently get the smallest current node
  • Divide-and-conquer approach as an alternative with same complexity
  • Handling edge cases: empty lists, null heads, and lists with duplicate values
  • Stability of the merge (if required) and whether to use iterative or recursive implementation

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

Q3

Deep copy a linked list where each node has a next pointer and a random pointer that can point to any node in the list or null.

Algorithms & Data Structures
Author's notes

The random pointer is what makes this annoying.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then propose a solution using a hash map to map original nodes to their copies, allowing O(n) time and space. Alternatively, describe the O(1) space interleaving approach if the interviewer wants optimization. Walk through the steps with a small example to demonstrate correctness.

Pro tip: Mention that the hash map approach is straightforward but uses O(n) extra space, and then offer the interleaving method as an optimization to show depth. This demonstrates you can balance clarity and efficiency, which Amazon values.

1. Clarify requirements and edge cases

Ask if the list can be empty, if random pointers can be null, and if modifying the original list is allowed. Confirm that deep copy means new nodes with same values and pointer structure.

2. Choose an approach

Decide between hash map (O(n) space) and interleaving (O(1) space). Explain the trade-offs and pick one based on constraints or interviewer preference.

3. Outline the algorithm

For hash map: traverse original, create copy nodes, store mapping; then set next and random pointers using the map. For interleaving: insert copy nodes after originals, set random pointers, then separate the lists.

4. Walk through an example

Use a small list (e.g., 3 nodes with random pointers) to trace the steps, showing how pointers are updated and ensuring no cycles or lost references.

5. Analyze complexity and test

State time and space complexity. Discuss potential bugs (e.g., null handling) and how to test with edge cases like empty list, single node, random pointing to self or null.

Key Points to Mention

  • Hash map mapping original nodes to their copies for O(n) time and space.
  • Interleaving approach for O(1) space by weaving copies into the original list.
  • Handling of null random pointers and edge cases like empty list.
  • Time complexity O(n) and space complexity O(n) or O(1) depending on approach.
  • Importance of not modifying the original list if required (or restoring it if using interleaving).
  • Testing strategy: verify deep copy by checking that no pointers reference original nodes.

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