Hashtables

Java: Hashmap (Hashtable class is deprecated). C#: Dictionary (Hashtable class is deprecated too). /!\ A hash table does not guarantee that the order will remain constant over time. Hashmap in Java final Map<String, String> myLookupTable…

Are Strings Anagrams?

Anagram = all letters can be reused to create another word. Approaches Approach 1: sorting (nonintuitive to me) An anagram is produced by rearranging the letters. Sort the letters alphabetically for the two strings. The sorted strings should…

Consistent hashing

Before diving into consistent hashing, let's understand the naive approach first. Naive: Distributed hashtable based on modulo Sharding can be achieved via a simple modulo shardNumber = hash(key) % TOTAL_SHARDS The hash function might differ…

Facebook friends

This question is similar to the "Route Between Nodes" from this book. Problem Consider a popular social networking website, Facebook for example. People can create their own profile and add friends. Given two Facebook profiles, I want to…

Validate BST

My usual mistake for this problem: You can not just compare node.left.value < node.value < node.right.value. This does not handle edge cases such as: 10 / \ 5 20 \ 20 20…