This is a premium problem. We're working on making it available for free soon.
Explore Free ProblemsSolutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, this type of problem can appear in interviews because it tests string manipulation, hexadecimal conversion, and mathematical reasoning. While categorized as easy, it checks whether candidates recognize patterns like multiples of 17.
No complex data structure is required for this problem. Simple string manipulation and integer math are sufficient to convert hexadecimal values and compute the nearest shorthand representation.
In shorthand RGB notation, a digit like 'A' expands to 'AA'. In decimal, this equals 17 multiplied by the digit value. Therefore all shorthand RGB pairs follow the pattern k × 17, where k ranges from 0 to 15.
The optimal approach converts each RGB pair into its decimal value and rounds it to the nearest multiple of 17. Each shorthand component corresponds to values like 00, 11, 22, ..., FF. This allows you to compute the closest shorthand color in constant time.