QuizzesArrays & Hashing
beginner
dsa

Arrays & Hashing

Master the HashMap pattern — the foundation of O(n) solutions for Two Sum, frequency counting, and duplicate detection.

25 minutes 10 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

10 questions on Arrays & Hashing. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What is the average time complexity of a HashMap lookup in Java?

    • AO(log n)
    • BO(n²)
    • CO(n)
    • DO(1)
  2. 2.Why does the Two Sum problem use a HashMap instead of a nested loop?

    • ATo sort the array first
    • BTo avoid modifying the original array
    • CTo reduce time from O(n²) to O(n) by storing previously seen numbers and their indices
    • DTo handle negative numbers correctly
  3. 3.You need to count how many times each word appears in a sentence. Which Java class is most appropriate?

    • AHashMap
    • BTreeSet
    • CArrayList
    • DPriorityQueue
  4. 4.Which method inserts a key-value pair into a HashMap only if the key is not already present?

    • Aadd()
    • BputIfAbsent()
    • Cmerge()
    • Dput()
  5. 5.What does this code print?

    Map<String, Integer> freq = new HashMap<>();
    String[] words = {"a", "b", "a", "c", "b", "a"};
    for (String w : words) {
        freq.put(w, freq.getOrDefault(w, 0) + 1);
    }
    System.out.println(freq.get("a"));
    • A1
    • B2
    • C3
    • Dnull
  6. 6.What is a hash collision?

    • ATwo different keys map to the same bucket index
    • BTwo identical keys with different values
    • CA key that cannot be hashed
    • DAn overflow in the hash table
  7. 7.What does it mean for a HashMap to be "unordered"?

    • AYou cannot retrieve values by key
    • BThe iteration order of keys is not guaranteed to match insertion order or sorted order
    • CKeys are stored in a circular list
    • DKeys are stored in reverse alphabetical order
  8. 8.What is the default initial capacity of a HashMap in Java?

    • A10
    • B8
    • C32
    • D16
  9. 9.You want to find all characters in a string that appear exactly once, preserving their original order. Which data structure helps?

    • ATreeSet
    • BPriorityQueue
    • CLinkedHashMap
    • DStack
  10. 10.What is the output?

    Set<Integer> seen = new HashSet<>();
    int[] nums = {1, 2, 3, 2, 1};
    int duplicates = 0;
    for (int n : nums) {
        if (!seen.add(n)) duplicates++;
    }
    System.out.println(duplicates);
    • A2
    • B3
    • C5
    • D0