FastPrepInsert a Doubly Linked List Node at a Position

Insert a Doubly Linked List Node at a Position

Qualys logoQualys● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given the values of a doubly linked list, a zero-based position, and a new value, insert a node before the current node at that position. Position values.length appends at the tail. Return the values in forward order.

For the practice runner, values serializes the list from head to tail. Build or reason about the doubly linked structure, connect both directions around the new node, update the head when needed, and return the serialized result.

Function

insertAtPosition(values: int[], position: int, value: int) → int[]

Examples

Example 1

values = [1,3,4]position = 1value = 2return = [1,2,3,4]

The new node is connected between 1 and 3.

Example 2

values = []position = 0value = 7return = [7]

Inserting into an empty list creates its head and tail.

Constraints

  • 0 <= values.length <= 10^5.
  • -10^9 <= values[i], value <= 10^9.
  • 0 <= position <= values.length.

More Qualys problems

See Qualys hiring insights
public int[] insertAtPosition(int[] values, int position, int value) {
    // Write your solution here.
}
values[1,3,4]
position1
value2
expected[1,2,3,4]
Checking account…