Ctrl K

Insert at Given Positions

Easy
You get two arrays of the same length, values and positions. Start with an empty list and walk through the arrays from left to right: at step i, insert values[i] so that it ends up at position positions[i] (0-based), shifting the element at that position and everything after it one place to the right. Return the list you get after all the inserts. Every position is guaranteed to be valid: at step i it never exceeds the current length of the list.
Example 1
values=[5, 6, 7, 8], positions=[0, 0, 1, 3] → [6, 7, 5, 8]

[5] → 6 at the front: [6, 5] → 7 at position 1: [6, 7, 5] → 8 at the end (position 3): [6, 7, 5, 8].

Example 2
values=[1, 2, 3], positions=[0, 0, 0] → [3, 2, 1]

Each number goes to the front, so the order comes out reversed.

Constraints
  • 1 ≤ len(values) = len(positions) ≤ 100
  • 0 ≤ values[i] ≤ 100
  • 0 ≤ positions[i] ≤ i
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHow Long the Light Stayed On EasyRepack the Tray EasyQuiz Score Log EasyMirror and Negate a Bitmap EasySwap Rows and Columns Easy
loading Python…
Input
values=[5, 6, 7, 8], positions=[0, 0, 1, 3]
Expected
[6, 7, 5, 8]

Run checks the first 3 tests in your browser. Submit checks all 71 tests on the server, hidden ones included.