Find the closest RGB color to a target color by Euclidean distance

Quick Overview

Given a list of RGB colors and a target color, find the color in the list with the smallest Euclidean distance to the target, keeping the earliest one when distances tie. It tests a clean linear scan, exact numeric comparison, and careful handling of a tie-breaking rule.

Find the closest RGB color to a target color by Euclidean distance

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a list of colors `colors`, where each color is represented as an RGB triple `[r, g, b]`, and a `target` color also represented as `[r, g, b]`, find the color in the list that is closest to the target based on the Euclidean distance in RGB space. The Euclidean distance between two colors `[r1, g1, b1]` and `[r2, g2, b2]` is defined as: $$\sqrt{(r_1 - r_2)^2 + (g_1 - g_2)^2 + (b_1 - b_2)^2}$$ Return the closest color from the list. If multiple colors have the same distance to the target, return the one that appears **first** in the list. ### Function Signature ```python def closest_color(colors: list[list[int]], target: list[int]) -> list[int]: ``` ### Rules - Every color, including `target`, is a list of three integers in the order red, green, blue. - Return the chosen color as a list `[r, g, b]` with the same values it has in `colors`. - When two or more colors tie for the smallest distance, return the one with the lowest index in `colors`. ### Constraints - `1 <= len(colors) <= 10^4` - Each color is a list of exactly `3` integers. - `0 <= r, g, b <= 255` - `target` is a list of exactly `3` integers with values in `[0, 255]`. ### Examples ```text Input: colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255]], target = [250, 10, 5] Output: [255, 0, 0] ``` The squared distances are 25 + 100 + 25 = 150 to red, 62500 + 60025 + 25 = 122550 to green, and 62500 + 100 + 62500 = 125100 to blue. Red is closest. ```text Input: colors = [[100, 100, 100], [200, 200, 200], [150, 150, 150]], target = [160, 160, 160] Output: [150, 150, 150] ``` Each channel of the three colors differs from the target by 60, 40 and 10, so the squared distances are 10800, 4800 and 300. The last color is closest. ```text Input: colors = [[13, 14, 10], [15, 10, 10], [10, 10, 16]], target = [10, 10, 10] Output: [13, 14, 10] ``` The first two colors are both at squared distance 25 (9 + 16 + 0 and 25 + 0 + 0), and the third is at 36. The tie goes to the color that appears first.

Overview: Given a list of RGB colors and a target color, find the color in the list with the smallest Euclidean distance to the target, keeping the earliest one when distances tie. It tests a clean linear scan, exact numeric comparison, and careful handling of a tie-breaking rule.

Google logo
Google
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a list of colors colors, where each color is represented as an RGB triple [r, g, b], and a target color also represented as [r, g, b], find the color in the list that is closest to the target based on the Euclidean distance in RGB space.

The Euclidean distance between two colors [r1, g1, b1] and [r2, g2, b2] is defined as:

(r1−r2)2+(g1−g2)2+(b1−b2)2\sqrt{(r_1 - r_2)^2 + (g_1 - g_2)^2 + (b_1 - b_2)^2}

Return the closest color from the list. If multiple colors have the same distance to the target, return the one that appears first in the list.

Function Signature

def closest_color(colors: list[list[int]], target: list[int]) -> list[int]:

Rules

  • Every color, including target , is a list of three integers in the order red, green, blue.
  • Return the chosen color as a list [r, g, b] with the same values it has in colors .
  • When two or more colors tie for the smallest distance, return the one with the lowest index in colors .

Constraints

  • 1 <= len(colors) <= 10^4
  • Each color is a list of exactly 3 integers.
  • 0 <= r, g, b <= 255
  • target is a list of exactly 3 integers with values in [0, 255] .

Examples

Input: colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255]], target = [250, 10, 5]
Output: [255, 0, 0]

The squared distances are 25 + 100 + 25 = 150 to red, 62500 + 60025 + 25 = 122550 to green, and 62500 + 100 + 62500 = 125100 to blue. Red is closest.

Input: colors = [[100, 100, 100], [200, 200, 200], [150, 150, 150]], target = [160, 160, 160]
Output: [150, 150, 150]

Each channel of the three colors differs from the target by 60, 40 and 10, so the squared distances are 10800, 4800 and 300. The last color is closest.

Input: colors = [[13, 14, 10], [15, 10, 10], [10, 10, 16]], target = [10, 10, 10]
Output: [13, 14, 10]

The first two colors are both at squared distance 25 (9 + 16 + 0 and 25 + 0 + 0), and the third is at 36. The tie goes to the color that appears first.