Given a callable function f(x, y) with a hidden formula and a value z, reverse engineer the formula and return all positive integer pairs x and y where f(x,y) == z. You may return the pairs in any order.
While the exact formula is hidden, the function is monotonically increasing, i.e.:
f(x, y) < f(x + 1, y)f(x, y) < f(x, y + 1)The function interface is defined like this:
interface CustomFunction {
public:
// Returns some positive integer f(x, y) for two positive integers x and y based on a formula.
int f(int x, int y);
};
We will judge your solution as follows:
9 hidden implementations of CustomFunction, along with a way to generate an answer key of all valid pairs for a specific z.function_id (to determine which implementation to test your code with), and the target z.findSolution and compare your results with the answer key.Accepted.Example 1:
Input: function_id = 1, z = 5 Output: [[1,4],[2,3],[3,2],[4,1]] Explanation: The hidden formula for function_id = 1 is f(x, y) = x + y. The following positive integer values of x and y make f(x, y) equal to 5: x=1, y=4 -> f(1, 4) = 1 + 4 = 5. x=2, y=3 -> f(2, 3) = 2 + 3 = 5. x=3, y=2 -> f(3, 2) = 3 + 2 = 5. x=4, y=1 -> f(4, 1) = 4 + 1 = 5.
Example 2:
Input: function_id = 2, z = 5 Output: [[1,5],[5,1]] Explanation: The hidden formula for function_id = 2 is f(x, y) = x * y. The following positive integer values of x and y make f(x, y) equal to 5: x=1, y=5 -> f(1, 5) = 1 * 5 = 5. x=5, y=1 -> f(5, 1) = 5 * 1 = 5.
Constraints:
1 <= function_id <= 91 <= z <= 100f(x, y) == z will be in the range 1 <= x, y <= 1000.f(x, y) will fit in 32 bit signed integer if 1 <= x, y <= 1000.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves trying every possible combination of positive integer values for the given equation. We systematically check each pair of numbers to see if they satisfy the equation. If they do, we've found a solution.
Here's how the algorithm would work step-by-step:
def find_solution(custom_function): solutions = []
max_value = 10 # Limit search space
for first_variable in range(1, max_value + 1):
for second_variable in range(1, max_value + 1):
# Evaluate the function for the current pair
result = custom_function.f(first_variable, second_variable)
# Check if the equation is satisfied, target is assumed to be zero implicitly.
if result == 0:
solutions.append([first_variable, second_variable])
return solutionsThe core idea is to leverage the equation's increasing nature. We can start from opposite ends and move inwards, intelligently eliminating possibilities that cannot be solutions.
Here's how the algorithm would work step-by-step:
class Solution:
def findSolution(self, customfunction, z_value):
x_value = 1
y_value = 1000
solution_list = []
# Iterate until x exceeds its max or y falls below its min
while x_value <= 1000 and y_value >= 1:
result = customfunction.f(x_value, y_value)
# If result is greater than target, decrement y
if result > z_value:
y_value -= 1
# If result is less than target, increment x
elif result < z_value:
x_value += 1
# Found a solution
else:
solution_list.append([x_value, y_value])
x_value += 1
# Need to increment x and continue search
return solution_list| Case | How to Handle |
|---|---|
| z is less than the minimum possible value of f(1,1) | Return an empty list immediately, as no solution can exist for z values that are too small. |
| f(x, y) always returns a value greater than z for all possible x and y within reasonable bounds | The binary search or two-pointer approach should naturally terminate and return an empty list when no solutions are found. |
| f(x, y) encounters integer overflow during calculation | Check for potential overflow and return an empty list if overflow occurs, or use a language feature to allow for larger number storage. |
| Maximum possible value of x or y is reached before finding a solution | Set a reasonable upper bound on x and y, and terminate the search if these bounds are reached without finding a solution. |
| z is a very large value that requires large x and y to satisfy the function | The search algorithm should handle large z efficiently by using binary search or a two-pointer approach, rather than a naive exhaustive search. |
| f(x, y) has multiple solutions. x and y can switch values but results in the same answer. | The algorithm must correctly identify and return all unique pairs of [x, y] that satisfy the equation without duplicates or omissions. |
| The function f(x,y) is computationally expensive | Employ a binary search or two-pointer approach to minimize the number of calls to f(x, y). |
| z is equal to the maximum possible value of f(x,y) within the given bounds. | The algorithm must correctly identify the solution at the boundary and return it without going out of bounds. |