Amazon Interview Question

Given a string, find the length (or longest substring) without repeating characters.

Interview Answer

Anonymous

Jun 24, 2026

I started by explaining the straightforward brute-force approach, where all possible substrings are generated and checked for duplicate characters. I discussed its time complexity and why it would not scale well for large inputs. I then proposed the optimal sliding window (two-pointer) approach using a hash set/hash map to efficiently maintain the current window of unique characters. As I coded the solution in C++, I explained how the left and right pointers move, how duplicates are handled, and why the algorithm runs in linear time. Finally, I walked through a sample example to verify the correctness of the implementation and discussed the time and space complexity.