I knew the sliding window approach going in, but explaining the hashmap logic out loud while coding it cleanly is harder than it sounds.
Use a sliding window with two pointers to maintain a window of unique characters, expanding the right pointer and shrinking the left when a duplicate is found. Track the maximum window length seen. This yields O(n) time and O(min(n, alphabet)) space.
Pro tip: At Amazon, emphasize scalability and edge cases: discuss how your solution handles large inputs and Unicode characters, and mention that you'd test with empty strings and all unique characters.
Ask about character set (ASCII vs Unicode), case sensitivity, and expected input size to determine constraints.
Explain that you'll use two pointers (left and right) to represent a window of unique characters, and a hash map to store the last seen index of each character.
Iterate the right pointer through the string; if the current character is in the map and its index is >= left, move left to that index + 1. Update the map with the current index and compute the window length.
State that time complexity is O(n) since each character is visited at most twice, and space complexity is O(min(n, m)) where m is the alphabet size.
Run through edge cases like empty string, all unique characters, and strings with repeating patterns to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.