CodingNeed.

Engineering practice · 25 MIN

Find the longest unique-character window

Maintain a moving invariant instead of rebuilding every substring.

Track the last index at which each character appeared. When a duplicate falls inside the active window, advance the left boundary past its previous occurrence. The boundary must never move backwards. Array.from iterates Unicode code points, so this exercise treats an emoji code point as one item; user-perceived grapheme clusters require a different segmentation policy.

Treat the function as a small service: define a contract, maintain an invariant, and test the boundaries.

Read the example

console.log(Array.from("a😀a").length);
Check the expected output
3

Your challenge

Return the maximum number of distinct consecutive Unicode code points in the input string.

Solution cost: Expected O(n) time · O(n), including the code-point array. space

Common trap

Using left = previous + 1 unconditionally moves the boundary backwards for abba.

Further reading: MDN: string iteration

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.