As reported: PayPal Senior Software Engineer Frontend, final bar raiser round (2025, 45 minutes, DSA + JavaScript): given a string, remove a minimum-length substring so that the remaining string has no duplicate characters (that is, keep the remaining string as long as possible); 15 test cases.
Given a string s, remove exactly one contiguous substring (possibly empty) so that the characters left — the part before it followed by the part after it — are all different. Return the minimum length of the removed substring.
Example 1
s = "aabcc" -> 3 remove "abc" (indices 1..3), leaving "ac"
Example 2
s = "abca" -> 1 remove the last "a", leaving "abc" (removing the first "a" also works)
Example 3
s = "aa" -> 1 leaving "a"
Example 4
s = "abc" -> 0 nothing to remove
Constraints: 1 ≤ s.length ≤ 10^5; s consists of lowercase English letters.
Code runner format: one line, s as a JSON string. Implement minRemovalLength(s); the harness prints the length.
Clarifications
- The removed part must be one contiguous block; removing characters from two separate places is not allowed.
- Return only the length: when several substrings of that length work, any of them is fine.