Design a linked list with indexed get, insert and delete

Quick Overview

Implement a singly or doubly linked list class that reads a value by index, inserts at the head, the tail or any index, and deletes by index. It tests pointer updates, index bounds checks and the cost of each operation.

Design a linked list with indexed get, insert and delete

Company: Nokia

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design your implementation of a linked list. You can choose to use a singly or doubly linked list. A node in a singly linked list should have two attributes: `val` and `next`. `val` is the value of the current node, and `next` is a pointer/reference to the next node. If you want to use a doubly linked list, you will also need an attribute `prev` to indicate the previous node in the list. Assume all nodes in the list are 0-indexed. Implement the following methods: - `get(index: int) -> int`: Get the value of the index-th node in the linked list. If the index is invalid, return -1. - `addAtHead(val: int)`: Add a node of value `val` before the first element of the linked list. After insertion, the new node will be the first node of the linked list. - `addAtTail(val: int)`: Append a node of value `val` as the last element of the linked list. - `addAtIndex(index: int, val: int)`: Add a node of value `val` before the index-th node in the linked list. If `index` equals the length, the node is appended at the end. If `index` is greater than the length, the node will not be inserted. - `deleteAtIndex(index: int)`: Delete the index-th node in the linked list, if the index is valid. In the examples, the first input line lists the calls in order (starting with the `MyLinkedList` constructor), the second line gives each call's arguments, and the output holds each call's return value, with `null` for calls that return nothing. Example 1: ```text Input: ["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"] [[], [1], [3], [1, 2], [1], [1], [1]] Output: [null, null, null, null, 2, null, 3] Explanation: list = MyLinkedList() # list: [] list.addAtHead(1) # list: [1] list.addAtTail(3) # list: [1, 3] list.addAtIndex(1, 2) # list: [1, 2, 3] list.get(1) # returns 2 (value at index 1) list.deleteAtIndex(1) # list: [1, 3] list.get(1) # returns 3 (value at index 1 after deletion) ``` Example 2: ```text Input: ["MyLinkedList", "addAtHead", "addAtHead", "addAtHead", "addAtIndex", "deleteAtIndex", "addAtHead", "addAtTail", "get"] [[], [7], [2], [1], [3, 0], [2], [6], [4], [4]] Output: [null, null, null, null, null, null, null, null, 4] Explanation: list = MyLinkedList() # list: [] list.addAtHead(7) # list: [7] list.addAtHead(2) # list: [2, 7] list.addAtHead(1) # list: [1, 2, 7] list.addAtIndex(3, 0) # list: [1, 2, 7, 0] list.deleteAtIndex(2) # list: [1, 2, 0] list.addAtHead(6) # list: [6, 1, 2, 0] list.addAtTail(4) # list: [6, 1, 2, 0, 4] list.get(4) # returns 4 (value at index 4) ``` ```hint Changes at index 0 Insertion and deletion both need the node just before the target position. Decide what plays that role when the target is index 0, so the front of the list does not need its own branch. ``` ```hint Checking the index Track how many nodes the list holds so you can reject a bad index before walking. `addAtIndex` accepts an index equal to the length, while `get` and `deleteAtIndex` do not. ``` ### Constraints & Assumptions - `0 <= index, val <= 1000` - Please do not use the built-in `LinkedList` library. - At most `2000` calls will be made to `get`, `addAtHead`, `addAtTail`, `addAtIndex` and `deleteAtIndex`. ### What a Strong Answer Covers - The node structure and the list-level state (head reference, length) the methods share. - Index edge cases for each method: index 0, an index equal to the length, and an index past the end. - Pointer updates for insertion and deletion that keep the rest of the list reachable and leave the length accurate. - The time and space cost of each method, and how singly versus doubly linked nodes change it. ### Follow-up Questions - How would you make `addAtTail` run in constant time, and what extra bookkeeping would `deleteAtIndex` need? - With a doubly linked list, how would `get` use the end closer to the index, and what does that do to the worst case? - How would you add a method that reverses the list in place? - What would you change if several threads called these methods at the same time?

Overview: Implement a singly or doubly linked list class that reads a value by index, inserts at the head, the tail or any index, and deletes by index. It tests pointer updates, index bounds checks and the cost of each operation.

Nokia logo
Nokia
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design your implementation of a linked list. You can choose to use a singly or doubly linked list.

A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node. If you want to use a doubly linked list, you will also need an attribute prev to indicate the previous node in the list. Assume all nodes in the list are 0-indexed.

Implement the following methods:

  • get(index: int) -> int : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  • addAtHead(val: int) : Add a node of value val before the first element of the linked list. After insertion, the new node will be the first node of the linked list.
  • addAtTail(val: int) : Append a node of value val as the last element of the linked list.
  • addAtIndex(index: int, val: int) : Add a node of value val before the index-th node in the linked list. If index equals the length, the node is appended at the end. If index is greater than the length, the node will not be inserted.
  • deleteAtIndex(index: int) : Delete the index-th node in the linked list, if the index is valid.

In the examples, the first input line lists the calls in order (starting with the MyLinkedList constructor), the second line gives each call's arguments, and the output holds each call's return value, with null for calls that return nothing.

Example 1:

Input:
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[],             [1],         [3],         [1, 2],       [1],   [1],             [1]]

Output: [null, null, null, null, 2, null, 3]

Explanation:
list = MyLinkedList()      # list: []
list.addAtHead(1)          # list: [1]
list.addAtTail(3)          # list: [1, 3]
list.addAtIndex(1, 2)      # list: [1, 2, 3]
list.get(1)                # returns 2 (value at index 1)
list.deleteAtIndex(1)      # list: [1, 3]
list.get(1)                # returns 3 (value at index 1 after deletion)

Example 2:

Input:
["MyLinkedList", "addAtHead", "addAtHead", "addAtHead", "addAtIndex", "deleteAtIndex", "addAtHead", "addAtTail", "get"]
[[],             [7],         [2],         [1],         [3, 0],       [2],             [6],         [4],         [4]]

Output: [null, null, null, null, null, null, null, null, 4]

Explanation:
list = MyLinkedList()      # list: []
list.addAtHead(7)          # list: [7]
list.addAtHead(2)          # list: [2, 7]
list.addAtHead(1)          # list: [1, 2, 7]
list.addAtIndex(3, 0)      # list: [1, 2, 7, 0]
list.deleteAtIndex(2)      # list: [1, 2, 0]
list.addAtHead(6)          # list: [6, 1, 2, 0]
list.addAtTail(4)          # list: [6, 1, 2, 0, 4]
list.get(4)                # returns 4 (value at index 4)

Constraints & Assumptions

  • 0 <= index, val <= 1000
  • Please do not use the built-in LinkedList library.
  • At most 2000 calls will be made to get , addAtHead , addAtTail , addAtIndex and deleteAtIndex .

What a Strong Answer Covers Guidance

  • The node structure and the list-level state (head reference, length) the methods share.
  • Index edge cases for each method: index 0, an index equal to the length, and an index past the end.
  • Pointer updates for insertion and deletion that keep the rest of the list reachable and leave the length accurate.
  • The time and space cost of each method, and how singly versus doubly linked nodes change it.

Follow-up Questions Guidance

  • How would you make addAtTail run in constant time, and what extra bookkeeping would deleteAtIndex need?
  • With a doubly linked list, how would get use the end closer to the index, and what does that do to the worst case?
  • How would you add a method that reverses the list in place?
  • What would you change if several threads called these methods at the same time?