Replace Elements with Greatest Element on Right Side

EasyArrays & Hashingsimulation

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

Given an integer array arr, build a result where position i holds the largest value among the elements strictly to the right of index i. The last position has nothing to its right, so it holds -1. Return the resulting array (same length as arr). Target O(n) time by sweeping from the right.

Examples

Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]
Right of 17 the max is 18; right of 18 the max is 6; ... ; the last slot is -1.
Input: arr = [400]
Output: [-1]
A single element has nothing to its right.

Constraints

  • 1 <= arr.length <= 10^4
  • 1 <= arr[i] <= 10^5
  • The output has the same length as `arr`.

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 simulation problems