Hi, I recently appeared for a Google Phone Screen Round where I was asked the following question:
You're given the positions of n factories on a number line with an upper bound X.
First Part:
Given a city's position a, calculate the pollution as:
pollution = Σ (1 / |a - fi|) for all factory positions fi.
If a == fi (i.e., city and factory share the same position), I chose to return -1.
I solved this quickly, coded it, and dry-ran a test case for explanation.
Second Part:
Find the position on the line (between 0 and X) where the pollution is minimized.
I first explained the brute-force approach and coded it and dry-ran a test case for explanation.
The interviewer then asked for optimization. I initially thought the answer might be the median, but since it's a reciprocal function, she suggested I draw a graph. That helped me realize that the minimum pollution point lies somewhere between each pair of factories.
Follow-up:
She asked how to find the minimum pollution point between each consecutive factory pair. I suggested applying binary search.
Time ran out before I could code the optimized version, and she didn’t mention anything further.
Question: Based on this, do you think I might pass to the next round?