Google | Phone | Find the minimum range of A for each element of B that fits
Anonymous User
2199

An array, A = [ (1, 10), (2, 7), (3, 11), (4, 7)]
Another array B = [ 3, 4, 1, 10, 6, ..... ] (size of B >> A).
You need to find a range from A for each element of B that fits the element of B and the range is minimum.

For example, for the first element of B (which is 3), there are 2 ranges that fit which are (1, 10) and (2,7). [Note that, ranges are exclusive, not inclusive, that's why (3,11) is not a fit for 3]
Now, between the two ranges, (2,7) has the minimum gap between them [ 7 - 2 = 5 whereas 10 - 1 = 9].
So, for 3, the answer will be (2, 7). You have to find all the ranges for the rest of elements.

I tried bruteforce and then binary search, but none of them could satisfy the interviewer (the binary search basically didn't work for other test-cases). Does anyone know about this problem or any link of leetcode similar problem like this? Thanks!

Comments (11)