Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
Which statement about using a hash set versus sorting to detect duplicates in an array of n integers is correct?
- A. Both take O(n) worst-case time
- B. Hashing takes expected O(n) time and O(n) space; sorting takes O(n log n) time and can use O(1) extra space (heap sort)
- C. Sorting uses more space than hashing
- D. Hashing guarantees O(n) worst-case time
Correct answer: B. Hashing takes expected O(n) time and O(n) space; sorting takes O(n log n) time and can use O(1) extra space (heap sort)
Hash operations are O(1) only on average; an adversarial input can cause collisions. Sorting then scanning adjacent pairs is deterministic O(n log n) and can be done in place.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- How many contiguous subarrays of [4, 3, 1, 7, 5, 7, 4, 3, 2, 1] contain exactly 2 odd numbers? (Use exactly(k) = atMost(k) − atMost(k − 1).)
- Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed? python from collections import…
- An equilibrium index i has (sum of elements left of i) = (sum of elements right of i), excluding a[i]. Which indices of [−4, 5, 8, −4, −4…
- A hash table with open addressing has load factor α = 0.75 and uniform hashing. What is the expected number of probes for an UNSUCCESSFUL…
- Insertion sort is run on an array of n elements that has exactly k inversions. What is its running time?