Back to Amazon
Amazon logo

Coding rounds

Amazon coding rounds

5 timed coding challenges drawn from real loops. Submit a solution to see your test pass rate and AI feedback.

All coding rounds
faang 50:00+160 XP

K Closest Points to Origin

Given an array of `points` where `points[i] = [x, y]` and an integer `k`, return the `k` closest points to the origin (0, 0) by Euclidean distance. The answer may be in any order. Discuss the heap (O(n log k)) and quickselect (O(n) avg) approaches.

Examples

  • distance 8 < distance 10

    in: points=[[1,3],[-2,2]], k=1

    out: [[-2,2]]

  • two closest

    in: points=[[3,3],[5,-1],[-2,4]], k=2

    out: [[3,3],[-2,4]]

javascript
Loading editor...