Skip to main content

K Closest Points to Origin

Find k closest points to origin (0,0) Return in any order Example: [[1,3],[-2,2]], k=1 => [[-2,2]]

I skip the square root — squared distance orders the same. A size-k heap or one quickselect partition beats sorting everything.

Examples

You write

k_closest(points: List[List[int]], k: int)

  1. Example 1

    Input
    points = [[1,3],[-2,2]], k = 1
    returns
    [[-2,2]]
  2. Example 2

    Input
    points = [[3,3],[5,-1],[-2,4]], k = 2
    returns
    [[3,3],[-2,4]]

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Heaps

I reach for it when

  • You need the k largest or k smallest items.
  • You repeatedly take the best remaining item.

Say this before you type. Say what the heap holds, how big it stays, and what sits on top.

All 22 Heaps problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1K Closest Points to Originmedium

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.