CodingReported in a candidate post, Jul 2025
Check if array A is a subset of array B
Filed under BlackRock
Free read 1 of 3Confirm if A is a subset in the mathematical sense (all elements of A are in B) or if frequency matters.
The question
Given two arrays A and B, determine if every element in A is present in B. Assume elements are integers.
Constraints
- A and B can contain duplicates
- Time complexity should be O(N + M)
- Space complexity should be O(M) where M is the size of B
Examples
- Input
- A = [1, 2], B = [3, 1, 2, 4]
- Output
- True
- Why
- Both 1 and 2 exist in B.
Approach
Confirm if A is a subset in the mathematical sense (all elements of A are in B) or if frequency matters.
Insert all elements of B into a hash set for O(1) average lookup.
Iterate through A and check if each element exists in the hash set.
Solution
O(N + M) time, O(M) space
def is_subset(A, B):
set_b = set(B)
return all(x in set_b for x in A)
# Complexity: O(N+M) time, O(M) space
Tips
- What they are testing: Tests if you can handle frequency counts or hash sets correctly.
- Common mistake: Using nested loops resulting in O(N*M) time complexity.
Related BlackRock questions
Collected from public candidate posts and coding forums. Updated Nov 2026.