Solve listed algorithm problems
Company: Rippling
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: HR Screen
Quick Answer: This set of problems evaluates algorithmic problem-solving skills across selection and order-statistics, graph algorithms and traversals, connected-component analysis on irregular grids, optimization under exchange-rate transformations, and streaming data-structure design.
Part 1: Median of Two Sorted Arrays
Constraints
- 0 <= len(a), len(b) <= 200000
- At least one of the arrays is non-empty
- -10^6 <= element value <= 10^6
- Both arrays are already sorted in non-decreasing order
Examples
Input: ([1, 3], [2])
Expected Output: 2.0
Explanation: Combined sorted array is [1, 2, 3], so the median is 2.
Input: ([1, 2], [3, 4])
Expected Output: 2.5
Explanation: Combined sorted array is [1, 2, 3, 4], so the median is (2 + 3) / 2.
Input: ([], [5])
Expected Output: 5.0
Explanation: Edge case where one array is empty.
Input: ([0, 0], [0, 0])
Expected Output: 0.0
Explanation: All values are the same, so the median is 0.
Hints
- Try partitioning the smaller array and infer the matching partition in the larger array.
- A correct partition makes every element on the left side less than or equal to every element on the right side.
Part 2: Maximize Value After Two Days of Currency Conversions
Constraints
- 1 <= number of distinct currencies across a day <= 200
- 0 <= len(day1), len(day2) <= 1000
- Each rate is a positive float
- No profitable cycle exists within a single day
Examples
Input: ('USD', [('USD', 'EUR', 0.9)], [('USD', 'EUR', 0.8)])
Expected Output: 1.125
Explanation: Day 1: USD -> EUR gives 0.9 EUR. Day 2: EUR -> USD gives 1 / 0.8 = 1.25. Final amount is 0.9 * 1.25 = 1.125.
Input: ('A', [('A', 'B', 2.0), ('B', 'C', 3.0)], [('A', 'C', 4.0), ('B', 'C', 1.0)])
Expected Output: 1.5
Explanation: Best is A -> B -> C on day 1 for 6.0 C, then C -> A on day 2 for 1 / 4 = 0.25, giving 1.5 A.
Input: ('USD', [('EUR', 'JPY', 130.0)], [('EUR', 'GBP', 0.8)])
Expected Output: 1.0
Explanation: Edge case: the starting currency is disconnected on both days, so doing nothing is optimal.
Input: ('USD', [('USD', 'EUR', 0.9), ('USD', 'GBP', 0.7)], [('USD', 'EUR', 0.95), ('USD', 'GBP', 0.5)])
Expected Output: 1.4
Explanation: EUR path returns about 0.947..., but GBP returns 0.7 * 2.0 = 1.4, which is best.
Hints
- Think of each day as a graph where path products represent conversion multipliers.
- If you know the best multiplier from `S` to every currency on day 1, and the best multiplier from every currency back to `S` on day 2, you can combine them.
Part 3: Find Bounding Boxes of Connected Houses in a Ragged Grid
Constraints
- 0 <= number of rows <= 200000
- Total number of existing cells across all rows <= 200000
- Each cell is either 0 or 1
Examples
Input: ([[1, 0, 1], [1, 0, 0], [0, 1]])
Expected Output: [(0, 0, 1, 0), (0, 2, 0, 2), (2, 1, 2, 1)]
Explanation: There are three components: the left vertical pair, the single top-right house, and the single bottom house.
Input: ([[1, 1, 0], [1], [0, 1, 1]])
Expected Output: [(0, 0, 1, 1), (2, 1, 2, 2)]
Explanation: The missing cell at row 1, col 1 breaks what would otherwise look connected in a rectangular grid.
Input: ([])
Expected Output: []
Explanation: Edge case: empty grid.
Input: ([[0], [], [1]])
Expected Output: [(2, 0, 2, 0)]
Explanation: Only one house exists.
Hints
- Treat each existing `1` cell as a graph node and run DFS or BFS over valid neighbors only.
- While exploring one component, keep track of min/max row and column indices.
Part 4: Compute the Maximum Transfer Time in a Network
Constraints
- 1 <= n <= 2000
- 0 <= len(edges) <= 5000
- 0 <= u, v < n
- The graph is connected
Examples
Input: (4, [(0, 1), (1, 2), (2, 3)])
Expected Output: 3
Explanation: A simple path of length 3 has diameter 3.
Input: (5, [(0, 1), (1, 2), (2, 3), (3, 0), (1, 4)])
Expected Output: 3
Explanation: The farthest pair is server 4 and server 3, at distance 3.
Input: (1, [])
Expected Output: 0
Explanation: Edge case: a single server has diameter 0.
Input: (5, [(0, 1), (0, 2), (0, 3), (0, 4)])
Expected Output: 2
Explanation: In a star, the distance between any two leaves is 2.
Hints
- In an unweighted graph, shortest paths from a source can be found with BFS.
- For a general graph, computing the diameter exactly can be done by running BFS from every node.
Part 5: Maintain the Median of a Data Stream
Constraints
- 1 <= len(operations) <= 200000
- Values inserted by `add` are integers
- Every `median` operation occurs only after at least one `add`
Examples
Input: ([('add', 1), ('median', None), ('add', 2), ('median', None), ('add', 3), ('median', None)])
Expected Output: [1.0, 1.5, 2.0]
Explanation: The medians after the three queries are 1, then (1+2)/2, then 2.
Input: ([('add', 5), ('add', 15), ('add', 1), ('add', 3), ('median', None)])
Expected Output: [4.0]
Explanation: Sorted values are [1, 3, 5, 15], so the median is (3 + 5) / 2.
Input: ([('add', 7), ('median', None)])
Expected Output: [7.0]
Explanation: Edge case: only one value has been inserted.
Input: ([('add', -1), ('add', -2), ('median', None), ('add', -3), ('median', None)])
Expected Output: [-1.5, -2.0]
Explanation: After two values the median is -1.5; after three values it is -2.
Hints
- Keep the smaller half of the numbers in a max-heap and the larger half in a min-heap.
- After every insertion, rebalance so the heaps differ in size by at most 1.