← Bloomberg Interview Insights
My first instinct was brute force O(n^2) and I coded it up fine, but then they asked about scale.
First, clarify the problem constraints and edge cases, then derive the condition for a valid request: for each A, B must satisfy max(0.5*A + 7, 0) < B <= A and B != A. Since B must be less than A, sort the array and for each A, count the number of elements in the range (0.5*A + 7, A) using binary search, summing these counts. This yields an O(n log n) solution.
Pro tip: Mention that the condition 'B is greater than half of A plus 7' can be rewritten as B > A/2 + 7, and since B <= A, this implies A > 14. Also, note that the problem counts directed requests, so each valid (A, B) pair is counted once for A as sender.
Restate the conditions for a valid friend request: B > A/2 + 7, B <= A, and A != B. Confirm that we need to count all such directed pairs (A, B) from the given array of ages.
For a given A, the valid B values are integers in (A/2 + 7, A]. Since B must be less than A, the range is (A/2 + 7, A). Also, note that A must be greater than 14 for the range to be non-empty.
Sort the array to enable binary search. For each A, use binary search to find the number of elements in the range (A/2 + 7, A). Sum these counts to get the total number of directed requests.
Consider cases where no valid requests exist (e.g., all ages <= 14). If there are duplicate ages, ensure that each occurrence is treated as a separate person, so counts are multiplied appropriately.
State that the time complexity is O(n log n) due to sorting and binary searches, and space complexity is O(1) or O(n) depending on sorting. Walk through a small example to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.