My first instinct was to reach for a sliding window and I started coding that up before realizing the cap at length 3 makes this way simpler.
Since the answer length is capped at 3, check for valid substrings of length 1, then 2, then 3, returning the first one found. For each length, scan the string from left to right and return the leftmost substring with all distinct characters. If none found, return an empty string.
Pro tip: Clarify with the interviewer whether the answer should be the substring itself or its length, and confirm the behavior when no valid substring exists (e.g., return empty string). This shows attention to detail and avoids wasted effort.
Confirm the return type (substring or length), the cap of 3, and what to return if no valid substring exists. Also consider empty string input.
Any single character is trivially distinct. Return the first character if the string is non-empty.
Scan the string from left to right; for each adjacent pair, check if the two characters are different. Return the first such pair.
Scan the string from left to right; for each triplet, check if all three characters are distinct. Return the first such triplet.
If no valid substring of length 1, 2, or 3 is found, return an empty string (or as clarified).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.