Google Interview experience
1 round over 2 weeks. No offer, meh experience.
I was asked to implement an optimal solution for the question below, but I couldn't come up with one at the time
Question:
You are given a list of distinct positive integers. From this list, you can select any subset of numbers, rearrange them in any order, and use the operators +, -, *, /, along with parentheses to create mathematical expressions. Need to find the smallest positive integer that can't be formed using any combination of these numbers, with each number being used at most once in any expression.
Constraints:
Can rearrange and reorder the selected numbers in any way.
Each number from the list can be used only once in a single expression.
Only results that are positive integers are considered valid.
Sample Input #1:
[1, 2, 6, 7]Sample Output:
4You can form the numbers 1, 2, and 3 using the elements.
The number 4 cannot be produced from any combination of these numbers with the allowed operators.
Sample Input #2:
[1, 2, 5]Sample Output:
41 (directly from the array)
2 (directly from the array)
3 = 1 + 2
In this example, you can form 1 and 2, but 4 is the lowest possible positive integer that cannot be formed using any combination of the numbers from the array.
The optimal solution involves using the greedy approach, which I wasn't able to think of on the spot
def find_smallest_missing_positive(arr):
arr.sort()
smallest_missing = 1
for num in arr:
if num <= smallest_missing:
smallest_missing += num
else:
break
return smallest_missing
arr = [1, 2, 5]
print(find_smallest_missing_positive(arr)) # Output: 4Time Complexity: O(n log n) due to the sorting step, where n is the number of elements in the array.
Space Complexity: O(1) for the greedy approach, as we use only a constant amount of extra space.
Know someone interviewing at Google? Pass it on.
Same company, different candidates.
Share yours while it’s fresh. It takes five minutes, it’s anonymous, and it will help future candidates.