Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Hashing, Two Pointers, and Sliding Windows (Summary)

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 Map and Set for keys that come from user data, and test for presence with has().
  • 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(), and splice() change the array you call them on.
  • Pass a comparator to sort() for numbers, and compare with ===.

Keep Practicing

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.