Solve listed algorithm problems
Company: Rippling
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: HR Screen
The interview note referenced several independent coding problems. Solve each one independently.
1. **Median of two sorted arrays**
You are given two integer arrays `a` and `b`, each already sorted in non-decreasing order. Compute the median of the multiset formed by all elements in both arrays. The target solution should run in `O(log(min(len(a), len(b))))` time.
2. **Maximize value after two days of currency conversions**
You start with `1.0` unit of an initial currency `S`. On day 1, you may perform any number of conversions using a set of exchange pairs and rates. On day 2, you may again perform any number of conversions using a different set of exchange pairs and rates. For each listed pair `(u, v)` with rate `r`, you may convert `u -> v` at rate `r` and `v -> u` at rate `1/r`. Determine the maximum amount of currency `S` you can end with after both days.
3. **Find bounding boxes of connected houses in a ragged grid**
You are given a 2D grid where rows may have different lengths. Each cell contains either `1` (house) or `0` (empty). Two houses are connected if they are adjacent vertically or horizontally and the neighboring cell actually exists in the ragged grid. For every connected component of houses, return its bounding box as `(min_row, min_col, max_row, max_col)`.
4. **Compute the maximum transfer time in a network**
You are given an undirected, unweighted network of servers. Sending a file across one edge takes one unit of time. If a file starts at one endpoint of the network's longest shortest path, what is the maximum time needed for the farthest server to receive it? In other words, compute the diameter of the graph/tree using graph traversal.
5. **Maintain the median of a data stream**
Design a data structure supporting:
- `add(x)`: insert a number
- `median()`: return the median of all inserted numbers
The operations should support online processing of a stream of values, with efficient insertion and constant-time median lookup.
Explain the expected time and space complexity for each problem.
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
You are given two integer arrays `a` and `b`, each already sorted in non-decreasing order. Return the median of all values from both arrays combined.
Your solution should run in `O(log(min(len(a), len(b))))` time.
The median is the middle value when the total number of elements is odd, or the average of the two middle values when it is even.