Data-structure design coding interview questions 10 questions in 1 chapter

Ten data-structure design questions in Java and Python: caches, hash tables, tries, a time map, a feed and a trip tracker.

Part of Applied AI Engineer (LLM) coding learning path

  1. 01Simple state and hash tables10 questions

Simple state and hash tables

Fixed slot counts, a HashSet from scratch, and O(1) insert, delete and getRandom.

  • 1.1 Design Parking System Easy 3 min

    I store the remaining empty slots for big, medium and small cars. addCar decrements the matching counter when it is still positive and returns true, or returns false when that size is full. There is no movement between sizes.

    Read the full answer
  • 1.2 Design HashSet Easy 4 min

    I implement a set with an array of buckets, each a list of keys. The bucket index is key modulo the table size. add appends the key if it is not already there, remove deletes it from the list, and contains walks the same bucket. This is the standard separate-chaining HashSet.

    Read the full answer
  • 1.3 Insert Delete GetRandom O(1) Medium 4 min

    I store the values in an array and a hash map from value to index. insert appends and records the index. remove swaps the target with the last element, updates the map, and pops the tail, so deletion stays constant time. getRandom then picks a uniform index in the compact array.

    Read the full answer
  • 1.4 LRU Cache Medium 5 min

    I combine a hash map with a doubly linked list. The map gives O(1) lookup of a node, and the list keeps the most recently used key at the front. get and put both move a key to the front, and when the cache is over capacity I evict the node at the tail.

    Read the full answer
  • 1.5 LFU Cache Hard 5 min

    I keep a map from key to a node, and a map from frequency to a linked set of keys in LRU order. get and a successful put bump the key's frequency and move it to the next frequency list. When the cache is full I evict the least recently used key in the lowest frequency list, then I insert the new key at frequency one.

    Read the full answer
  • 1.6 Time Based Key-Value Store Medium 4 min

    I store a list of (timestamp, value) pairs for each key, appended in increasing timestamp order. set just appends. get binary-searches that list for the rightmost timestamp that is less than or equal to the query time, and returns the empty string if none exists.

    Read the full answer
  • 1.7 Implement Trie (Prefix Tree) Medium 4 min

    I store the words in a tree where each edge is a character and each node has a flag for "a word ends here". insert walks or creates the path. search requires that the final node is marked as a word, and startsWith only requires that the path exists.

    Read the full answer
  • 1.8 Design Add and Search Words Data Structure Medium 4 min

    I insert words into a trie the same way as a prefix tree. search walks the trie, and when the pattern has a '.' it tries every child at that step. A match is valid only if we consume the whole pattern and land on a node marked as a word.

    Read the full answer
  • 1.9 Design Twitter Medium 5 min

    I give every tweet a global increasing timestamp, I store each user's tweets, and I store each user's followees. A news feed is a 10-wide merge of that user's tweets and the tweets of the people they follow, using a max-heap of the latest tweet from each person. follow and unfollow just update the followee set.

    Read the full answer
  • 1.10 Design Underground System Medium 6 min

    I record each in-progress check-in by customer id as a station and a time. On check-out I look up that check-in, add the travel time to a running sum and count for the station pair, and I remove the check-in. getAverageTime then returns sum divided by count for that pair.

    Read the full answer