← LinkedIn Interview InsightsAI Engineer

LinkedIn·AI Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026Remote

Summary

Interviewed for an AI Engineer role at LinkedIn and got a math-heavy algorithms question that I wasn't fully prepared to prove rigorously on the spot. The coding part was manageable but the proof component threw me.

Questions Asked (1)

Q1

Given an array of 1D coordinates, find a point c that minimizes the total sum of absolute distances to all points. Prove mathematically that the optimal c is the median, and implement a solution in better than O(n log n) time if possible.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The coding part I was fine with.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by restating the problem and clarifying assumptions (1D points, sum of absolute distances). Explain that the objective function is convex and piecewise linear, so the minimum occurs where the subgradient contains zero, which is at the median. Then discuss time complexity: sorting takes O(n log n), but selection algorithms like Quickselect or Median of Medians can find the median in O(n) average or worst-case time.

Pro tip: Mention that for even n, any point between the two middle values minimizes the sum, and that the median approach extends to higher dimensions via the geometric median (though not simply coordinate-wise medians). This shows depth beyond the basic proof.

1. Clarify and formalize

Restate the problem: given points x1,...,xn, minimize f(c)=Σ|xi-c|. Confirm that c can be any real number and that we seek the exact minimum.

2. Prove optimality of median

Show that f is convex and piecewise linear with slope changes at each xi. The subgradient is zero when the number of points left of c equals the number right, i.e., c is a median. Alternatively, use the pairing argument: moving c away from the median increases distances to more points than it decreases.

3. Handle even n and ties

Explain that for even n, any c between the two middle values yields the same minimum sum, so the median is not unique but the minimum value is.

4. Discuss algorithms and complexity

Sorting gives O(n log n). To achieve better, use Quickselect (average O(n)) or Median of Medians (worst-case O(n)) to find the median, then compute the sum in O(n).

5. Implement and test

Write code for the O(n) selection-based solution, handle edge cases (empty array, single element), and verify with examples.

Key Points to Mention

  • Convexity of the absolute value function and the sum
  • Subgradient condition for optimality
  • Median as the minimizer (any point between middle two for even n)
  • Time complexity: O(n log n) via sorting vs O(n) via selection
  • Quickselect and Median of Medians algorithms
  • Edge cases: empty input, duplicates, even/odd lengths

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.