Solve six algorithmic problems
Company: LinkedIn
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This multi-part question evaluates algorithmic problem solving, data-structure selection, complexity analysis, API design, and the handling of trees, arrays, grids, circular structures, and dynamic updates.
Part 1: Bulb Toggling Passes
Constraints
- 0 <= n <= 10^18
- Bulbs are 1-indexed
- A toggle changes off to on, or on to off
Examples
Input: (0,)
Expected Output: 0
Explanation: There are no bulbs, so the answer is 0.
Input: (1,)
Expected Output: 1
Explanation: Bulb 1 is toggled once, so it stays on.
Hints
- A bulb ends up on only if it is toggled an odd number of times.
- Most divisors come in pairs. Which numbers have an unpaired divisor?
Part 2: Repeated Word-Distance Queries
Constraints
- 1 <= len(words) <= 10^5
- 1 <= len(queries) <= 10^5
- Each word is a non-empty string
- Total processing should avoid rescanning the full words array for every query
Examples
Input: (["practice", "makes", "perfect", "coding", "makes"], [("coding", "practice"), ("makes", "coding")])
Expected Output: [3, 1]
Explanation: coding is at index 3 and practice at 0, so distance 3. makes occurs at 1 and 4; the closest makes to coding is at 4, so distance 1.
Input: (["a", "b", "a"], [("a", "c")])
Expected Output: [-1]
Explanation: Word c does not appear.
Hints
- Store all positions for each distinct word in sorted order as you scan the list once.
- To compare two sorted position lists efficiently, try a two-pointer walk.
Part 3: Invert a Binary Tree
Constraints
- 0 <= number of list entries <= 10^4
- Node values are integers
- If level_order is empty, the tree is empty
Examples
Input: ([4, 2, 7, 1, 3, 6, 9],)
Expected Output: [4, 7, 2, 9, 6, 3, 1]
Explanation: Every left/right subtree pair is mirrored.
Input: ([2, 1, 3, None, None, 4],)
Expected Output: [2, 3, 1, None, 4]
Explanation: Node 3's single child moves from the left side to the right side after inversion.
Hints
- Inversion is local: at every node, swap the left and right pointers.
- A breadth-first traversal is an easy way to process every node iteratively.
Part 4: Count Land Clusters in a Grid
Constraints
- 0 <= number of rows <= 300
- 0 <= number of columns <= 300
- All rows have the same length
- Adjacency is only up, down, left, and right
Examples
Input: (["11000", "11000", "00100", "00011"],)
Expected Output: 3
Explanation: There are three separate land clusters.
Input: (["000", "000"],)
Expected Output: 0
Explanation: There is no land.
Hints
- When you find an unvisited land cell, explore its whole component with DFS or BFS.
- Mark cells as visited so you do not count the same island more than once.
Part 5: Longest Run in a Circular Binary Array
Constraints
- 0 <= len(nums) <= 2 * 10^5
- 0 <= k <= len(nums)
- Each element of nums is either 0 or 1
- The chosen window length cannot exceed len(nums)
Examples
Input: ([1, 1, 0, 1], 0)
Expected Output: (3, 3)
Explanation: The best circular run is indices 3 -> 0 -> 1, which gives three 1s.
Input: ([1, 0, 1, 1, 0], 1)
Expected Output: (4, 0)
Explanation: Flipping the zero at index 1 gives a length-4 window starting at 0. Another length-4 answer starts at 2, but 0 is smaller.
Hints
- A standard sliding window works for 'at most k zeros', but circularity suggests looking at a doubled array.
- Even with doubling, do not allow a window longer than the original array length.
Part 6: Peel Leaves by Rounds
Constraints
- 0 <= number of list entries <= 10^4
- Node values are integers
- The intended solution should run in O(n)
Examples
Input: ([1, 2, 3, 4, 5],)
Expected Output: [[4, 5, 3], [2], [1]]
Explanation: First remove leaves 4, 5, and 3; then 2 becomes a leaf; finally 1.
Input: ([1],)
Expected Output: [[1]]
Explanation: A single node is removed in the first round.
Hints
- Instead of physically removing leaves round by round, compute each node's height from the bottom.
- All nodes with the same bottom-up height are removed in the same round.