Solve Three Algorithm Variants
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This multi-part question evaluates combinatorics and permutation generation, tree traversal and node-relationship classification, and array/sequence processing under bounded-gap constraints, measuring algorithmic design, complexity reasoning, and data-structure manipulation within the Coding & Algorithms domain.
Part 1: Generate Queue Arrangements
Constraints
- 0 <= len(students) <= 8
- All student IDs are distinct integers
- Because the output size is factorial, n is intentionally small
Examples
Input: []
Expected Output: [[]]
Explanation: There is exactly one permutation of an empty list: the empty arrangement.
Input: [7]
Expected Output: [[7]]
Explanation: A single student has only one possible position.
Hints
- Think about building the arrangement one position at a time.
- Keep track of which students have already been used in the current partial arrangement.
Part 2: Generate Circular Queue Arrangements
Constraints
- 0 <= len(students) <= 9
- All student IDs are distinct integers
- Rotations are considered identical, but reversed orders are different
Examples
Input: []
Expected Output: [[]]
Explanation: There is one empty circular arrangement.
Input: [9]
Expected Output: [[9]]
Explanation: A single student forms exactly one circle.
Hints
- If rotations are the same, you can fix one student in place and only permute the others.
- Using the first input student as the fixed anchor makes the output deterministic.
Part 3: Classify the Relationship Between Two Tree Nodes
Constraints
- 0 <= number of nodes <= 100000
- All node values in the tree are unique integers
- The tree is rooted and acyclic
Examples
Input: ((1, [(2, [(4, []), (5, [])]), (3, [(6, []), (7, [])])]), 4, 5)
Expected Output: 'siblings'
Explanation: Nodes 4 and 5 share the same parent, node 2.
Input: ((1, [(2, [(4, []), (5, [])]), (3, [(6, []), (7, [])])]), 4, 6)
Expected Output: 'cousins'
Explanation: Nodes 4 and 6 are at the same depth but have different parents.
Hints
- For each target, you only need two facts: its parent and its depth.
- A single DFS or BFS traversal can collect both targets' information.
Part 4: Find the Longest Bounded-Gap Sequence
Constraints
- 0 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- 1 <= k <= 10^9
Examples
Input: ([], 3)
Expected Output: 0
Explanation: No numbers means no valid sequence.
Input: ([7], 5)
Expected Output: 1
Explanation: A single distinct value forms a sequence of length 1.
Hints
- Duplicates never help, so try removing them first.
- After sorting the distinct values, the answer becomes the longest consecutive block whose neighboring gaps are all less than k.