Sort Transformed Array

MediumTwo Pointerstwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array nums sorted in ascending order and integers a, b, c. Apply f(x) = a*x^2 + b*x + c to every element. Return the transformed values sorted in ascending order. Duplicates are kept. Try to do it in O(n) time instead of sorting again: a quadratic changes direction at most once, so the largest (or smallest) values sit at the ends of nums.

Examples

Input: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
Output: [3,9,15,33]
f gives [9, 3, 15, 33], which sorted is [3, 9, 15, 33].
Input: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
Output: [-23,-5,1,7]
f gives [-23, -5, 7, 1], which sorted is [-23, -5, 1, 7].

Constraints

  • 0 <= nums.length <= 200
  • -100 <= nums[i], a, b, c <= 100
  • `nums` is sorted in ascending order

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems