Interview Q&A / Data Structures & Algorithms

INTERVIEW Q&A · 55 QUESTIONS

Data Structures & Algorithms interview questions

Arrays, linked lists, hashing, trees and patterns, with line-by-line code and complexity.

  1. Two Sum - find two numbers that add up to a target

    Walk through the array once and store each number's index in a hash map. For each number, check if target minus that number is already in the map. This takes O(n) time.

    BeginnerIn-depthCoding2 min read
  2. How do you find the longest substring without repeating characters?

    Use a sliding window. Expand the right end one character at a time and remember the last index where each character was seen. If the new character already appears inside the window, move the left end just past its previous position. Track the largest window size. This is O(n) time.

    AdvancedIn-depthCoding4 min read
  3. How do you check whether two strings are anagrams of each other?

    Two strings are anagrams if they have the same letters with the same counts. Count the characters of the first string, subtract using the second, and check that every count ends at zero.

    BeginnerQuick answerCoding1 min read
  4. How do you find the first non-repeating character in a string?

    Count how often each character appears in one pass, then scan again and return the first character whose count is 1.

    BeginnerQuick answerCoding1 min read
  5. How does a hash map handle collisions?

    A collision happens when two keys hash to the same bucket. Chaining stores colliding entries in a list inside the bucket; open addressing probes for the next free slot. Both keep lookups near O(1) on average if the table stays sparse.

    BeginnerQuick answer1 min read
  6. When should you use a Map instead of a plain object in JavaScript?

    Use a Map when keys are not just strings, when you add and remove entries often, or when you need a reliable size and insertion order. Use a plain object for fixed, string-keyed records.

    BeginnerQuick answer1 min read
  7. How do you count subarrays whose sum equals k using a prefix sum and hash map?

    Keep a running prefix sum and a map of how many times each prefix sum has occurred. At each step, the number of subarrays ending here with sum k equals the count of earlier prefix sums equal to current minus k.

    IntermediateQuick answerCoding1 min read
  8. How do you find the length of the longest consecutive sequence in an unsorted array in O(n)?

    Put all numbers in a set. Only start counting from a number whose predecessor is not in the set, then walk upward while the next number exists. Each number is visited at most twice.

    IntermediateQuick answerCoding1 min read
  9. How do you group anagrams together from a list of words?

    Anagrams share the same sorted letters, so use the sorted word as a hash map key and collect each word under its key. The map values are the groups.

    IntermediateQuick answerCoding1 min read
  10. How do you design an LRU cache with O(1) get and put?

    Combine a hash map for instant lookup with an ordering of recent use. In JavaScript a Map remembers insertion order, so deleting and re-inserting a key on each access moves it to the newest end; the oldest key is evicted when full.

    AdvancedQuick answerCoding1 min read