My first instinct was to just map and sort, which is O(n log n) and they clearly wanted better.
Recognize that applying a quadratic to a sorted array produces a sequence that is either monotonic or has a single minimum/maximum, depending on the sign of 'a'. Use a two-pointer technique from both ends of the array, comparing the transformed values and building the result in sorted order in O(n) time.
Pro tip: Clarify edge cases upfront, such as a=0 (linear function) or negative 'a' (maximum instead of minimum), and mention that you can avoid recomputing f(x) by comparing based on the vertex and monotonicity. This shows attention to detail and efficiency.
Determine the shape of f(x) based on the sign of 'a': if a > 0, it's a parabola opening upwards (minimum); if a < 0, opening downwards (maximum); if a = 0, it's linear. This dictates whether the transformed array will have a minimum or maximum in the middle.
For a ≠ 0, use a two-pointer approach: start with pointers at the beginning and end of the sorted array, compare the transformed values at these pointers, and place the larger (or smaller, depending on 'a') at the end of the result array. For a = 0, simply apply the linear function and the array remains sorted if b > 0, or reverse if b < 0.
Initialize left = 0, right = n-1, and an output array of size n. While left <= right, compute f(arr[left]) and f(arr[right]), compare them, and fill the output from the end to the beginning. Move the pointer that gave the more extreme value inward.
Test with a = 0, negative 'a', empty array, and single-element array. Ensure the output is correctly sorted and that integer overflow is considered (use long integers if necessary).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.