← All questions
MediumCoding

Sum of Distances

Asked atSprinklr
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Clarify whether distance means graph distance, array-index distance, or coordinate distance.
  2. 02For equal-value index distances, group indices by value.
  3. 03Process each sorted group with running prefix sums.
  4. 04For coordinate pairs, sort and sum left and right contributions.

Reference answer

Then expect these follow-ups

  • Why are groups already sorted by index?

    Tests: observations

  • How does the formula change for coordinates?

    Tests: generalization

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question