You’ve practiced three patterns that turn slow, nested scans into single passes:
- Hashing trades O(n) extra memory for O(1) average lookups. Use
MapandSetfor keys that come from user data, and test for presence withhas(). - Two pointers use the order of sorted or mirrored data to drop the inner loop, often with O(1) extra space. Walk strings by code point, and keep a write index instead of calling
splice()in a loop. - Sliding windows update an answer as the edges move, so every item enters and leaves the window once. Keep counts honest by deleting keys that drop to zero, and keep money in whole cents.
Things to Remember in an Interview
- Get a correct solution first, then say what it costs and how you’d improve it.
- Ask whether you may modify the input.
sort(),reverse(), andsplice()change the array you call them on. - Pass a comparator to
sort()for numbers, and compare with===.