Unlabelled Seed Packet

MediumsortingAlso known as k-Nearest Neighbours Classification

A community seed library has a jar of seeds whose label fell off. Volunteers have measured many labelled seeds before (length and width in millimetres, sometimes more features), and they measure one seed from the mystery jar.

Their rule: find the k labelled seeds that are closest to the mystery seed, and go with the most common label among them. This is k-nearest neighbours, one of the simplest classifiers there is. The same "find the closest stored examples" step sits behind recommendation systems and embedding search.

Your task

Write classifySeed(seeds, labels, query, k). seeds[i] is a list of measurements with label labels[i]. query has the same number of measurements. Use straight-line (Euclidean) distance.

  1. Rank all seeds by distance to query, nearest first. If two seeds are equally far, the one with the lower index ranks first.
  2. Take the first k. Return the label that appears most often among them.
  3. If several labels tie for most votes, return the tied label whose highest-ranked seed comes first.

classifySeed(seeds: float[][], labels: string[], query: float[], k: int) → string

Examples

Example 1
Input
seeds = [[5, 2], [6, 2], [2, 1], [3, 1], [6, 3]]
labels = ["rye", "rye", "flax", "flax", "oat"]
query = [5, 3]
k = 3
Output
"rye"
Why

Squared distances to (5, 3) are 1, 2, 13, 8 and 1. Ranked: seed 0 (1), seed 4 (1, higher index), seed 1 (2). Their labels are rye, oat, rye. Rye has 2 votes, so the answer is "rye".

Example 2
Input
seeds = [[0, 0], [4, 0], [0, 5], [10, 10]]
labels = ["oat", "rye", "rye", "oat"]
query = [1, 0]
k = 2
Output
"oat"
Why

Distances: 1, 3, about 5.1 and about 13.5. The 2 nearest are seed 0 (oat) and seed 1 (rye), one vote each. The tie goes to the label whose seed ranks first: seed 0 is nearer, so the answer is "oat".

Example 3
Input
seeds = [[1], [2], [3], [10], [11]]
labels = ["a", "a", "a", "b", "b"]
query = [9]
k = 1
Output
"b"
Why

With one measurement, distance is just the gap. The nearest seed to 9 is 10, label "b". With k = 1 the nearest seed decides alone, even though most seeds overall are "a".

Constraints

  • 1 ≤ k ≤ seeds.length ≤ 1,000
  • 1 ≤ number of measurements ≤ 10
  • -1,000 ≤ each measurement ≤ 1,000
  • Labels are non-empty lowercase strings.
Start with brute force

Pick the nearest seed, then the nearest of the rest, and so on, k times. Each pick scans every seed, so it costs O(n · k), which becomes O(n²) when k is close to n.

Sorting once by distance costs O(n log n) and gives the whole ranking. The bigger risks here are the rules, not the speed: forgetting the index tie-break, breaking vote ties alphabetically, or voting over all seeds instead of the nearest k.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code