Closest Color
Updated
Problem statement
A color is represented as a 24-bit integer, with 8 bits each for red (R), green (G), and blue (B) components. Each component ranges from 0 for low intensity to 255 for high intensity.
The distance between two colors with RGB values (r1, g1, b1) and (r2, g2, b2) is:
d = sqrt((r1 - r2)^2 + (g1 - g2)^2 + (b1 - b2)^2)Pure colors
Compare each pixel with these five pure colors:
| Pure color | R | G | B |
|---|---|---|---|
| Black | 0 | 0 | 0 |
| White | 255 | 255 | 255 |
| Red | 255 | 0 | 0 |
| Green | 0 | 255 | 0 |
| Blue | 0 | 0 | 255 |
Given a list of 24-bit binary strings representing pixel color values, determine which pure color each pixel is closest to.
If a pixel is equally close to multiple colors, use "Ambiguous".
Function
closestColor(pixels: String[]) → String[]Examples
Example 1
pixels = ["000000001111111100111100"]return = ["Green"]Split the binary string into three components of 8 bits each:
R = "00000000", which is0.G = "11111111", which is255.B = "00111100", which is60.
Thus, the pixel's RGB value is (0, 255, 60).
The smallest distance is to Green, so the result is "Green".
Constraints
- Each value in
pixelsis a 24-bit binary string. - Each component is represented by
8bits and ranges from0to255.
More Two Sigma Online Assessment questions
- IPO Share AllocationOA · Seen Sep 2026
- Sewer Drainage PartitionOA · Seen Sep 2026
- Daily Temperature By TownOA · Seen Sep 2026
- Linear InterpolatorOA · Seen Sep 2026
- Online No-Intercept Linear RegressionOA · Seen Sep 2026
- Balanced Split String with WildcardsOA · Seen Aug 2026
- Piecewise Linear Interpolation and ExtrapolationOA · Seen May 2026
- Calculate y/x using PatchOA · Seen Nov 2025
- Nums That Are Divisible by N 🍋OA · Seen Mar 2024
- Replacing Val 🍓OA · Seen Mar 2024
Similar questions at other companies
- Count Multiples of Three with Two SevensHudson River Trading · OAHudson River Trading interview questions
- Price Deviation AlertsJane Street · OAJane Street interview questions
- Maximum Equal Parts for PrefixesAmazon · OA
- Most Cost-Effective MachineCapital One · ONSITE INTERVIEWCapital One interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Eight-Direction Keyboard Typing TimeBenchling · PHONE SCREENBenchling interview questions
- Picked Player Who LostSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Theme Park Shuttle Wait TimeAirbnb · OAAirbnb interview questions
- REST API: Country CodesHackerRank · OAHackerRank interview questions
- Total Cost Against the Lowest Earlier PricePayPal · OAPayPal interview questions