Test your knowledge of arrays and hash maps, focusing on deduplication, frequency counting, and their Big-O time and space complexities. This beginner-friendly quiz helps reinforce essential concepts and best practices in algorithm efficiency and data structure usage.
Given an array of integers, which data structure is commonly used to detect duplicates in O(n) time?
Explanation: A hash set allows for O(1) average time lookups, making it efficient for checking if an element has already appeared. Array lists do not provide constant time searches, so they can result in slower performance. Binary trees require O(log n) time per operation and are more complex for this use. Linked queues do not support fast lookups and are not suitable for deduplication.
Which approach efficiently counts how many times each value occurs in an integer array?
Explanation: A hash map can track the frequency of each element with O(1) insertions, leading to O(n) total time. Sorting and traversing works but is less efficient due to the sorting step. A stack does not support key-based access or frequency tracking. Recursion does not inherently provide a counting mechanism or improve efficiency here.
What is the average-case time complexity for removing duplicates from an array using a hash set?
Explanation: Each element can be checked or inserted into the hash set in O(1) on average, so the overall process is O(n). O(n^2) results from nested loops without hashing. O(log n) applies to balanced trees, not hash sets. O(1) is incorrect because it ignores scanning all elements.
If you want to extract unique integers from the array [1, 2, 2, 3], which structure lets you do so with O(n) time and stores only unique values?
Explanation: A hash set only keeps unique values and provides O(1) operations, ensuring O(n) time overall. Queues and stacks may store duplicates unless managed manually, losing the efficiency and property of uniqueness. A regular array does not enforce uniqueness.
To count how many times each word appears in a list of strings, which technique is most efficient?
Explanation: Mapping each word to a counter in a hash map takes O(1) time per word, making it efficient and straightforward. Simply adding words to an array does not count their occurrences. A binary search tree complicates insertion and may not guarantee O(1) time. Reversing the list does not assist in counting frequencies.
What is the space complexity of counting the occurrences of each distinct string in an array with n elements and k unique strings?
Explanation: O(k) space is used because each unique string occupies a slot in the frequency map. O(n^2) is incorrect as space does not scale quadratically here. O(1) space is not realistic as more unique elements require more storage. O(log n) is unrelated to the scenario.
If you want to remove duplicates and keep the original order of elements in an array, which approach is both efficient and order-preserving?
Explanation: Adding elements to a result list only if they are not yet in a hash set ensures both order preservation and efficiency. Sorting loses the original sequence. Reversing changes the order and does not guarantee uniqueness alone. Shuffling randomizes the order, which is not desired.
Why is a hash map preferred for frequency counting over arrays when element values can be very large or negative?
Explanation: Hash maps allow any kind of key, including large or negative numbers, while arrays rely on non-negative, contiguous indices. Arrays can only be faster if the possible keys are few and within array bounds, which is rare in such cases. Arrays cannot store arbitrary keys. Hash maps clearly store counts using key-value pairs.
If an array contains many repeated appearances of the same value, how does this affect space complexity when using a hash map for counting?
Explanation: A hash map stores only unique keys and their counts, so overall space relates to the count of unique elements. Each duplicate merely increases the value of an existing key, not the number of keys. Space does not become quadratic from duplicates alone. Duplicates do not reduce or save memory usage in this context.
What is the average-case time complexity for checking if a hash map contains a specific key?
Explanation: Hash maps offer average-case O(1) time for key lookups, making them highly efficient for frequency tasks. O(n) arises for linear searches, not hash maps. O(n^2) is unrelated to key checks. O(log n) applies to balanced search trees instead.
What is a potential drawback of removing duplicates from an original array in-place?
Explanation: Removing elements in-place may require shifting remaining values to fill gaps, which can be slow. Extra arrays are not always needed, so that answer is incorrect. Deduplication applies to integers just like other types. It does not automatically sort the array.
What is the worst-case space complexity when using a hash set to deduplicate an array of n elements?
Explanation: In the worst case, where all elements are unique, the hash set will store all n of them, so space is O(n). O(1) only applies if there are a constant number of unique items, which is not generic. O(log n) is unrelated to hash sets. O(n^2) is unnecessarily large for this purpose.
Why is using only an array inefficient for deduplication compared to a hash set or hash map?
Explanation: Without constant-time lookups, arrays require scanning through the list for each value, which is slow. Arrays can certainly store integers, so that distractor is incorrect. Arrays do not always use more memory. Hash maps do not sort by default, making that option irrelevant.
What is the time complexity of counting frequencies in an empty array using a hash map?
Explanation: An empty array leads to no operations, so the time complexity is considered O(1). O(n) would apply if there were n elements, which is not the case here. O(n^2) and O(log n) are incorrect as there is nothing to process.
If an array contains all unique values, how does this affect the efficiency of deduplication using a hash set?
Explanation: Even if all values are unique, each one is checked and added, resulting in O(n) time and space. O(1) is not accurate since each value needs processing. Deduplication is still valid for an all-unique array. Memory use is not reduced, because all elements must be stored in the set.
When analyzing a paragraph of text, why is a hash map practical for counting how often each letter appears?
Explanation: A hash map can track each letter and its count efficiently during a single scan. Ignoring uppercase letters is not automatic; that requires custom logic. Hash maps can handle any symbol provided as a key. Sorting by frequency is not inherent, as hash maps do not manage order.