Maintain a List with Indexed Insertions and Deletions

Quick Overview

Maintain a List with Indexed Insertions and Deletions. Implement MyLinkedList using your own singly or doubly linked nodes, indexed from zero.

Maintain a List with Indexed Insertions and Deletions

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement MyLinkedList using your own singly or doubly linked nodes, indexed from zero. get(index) returns the value or -1 for an invalid index. addAtHead and addAtTail insert at either end. addAtIndex(index,val) inserts before that index, appends when index equals the size, and does nothing when index exceeds it. deleteAtIndex removes a valid indexed node and otherwise does nothing. ```cpp class MyLinkedList { public: MyLinkedList(); int get(int index); void addAtHead(int val); void addAtTail(int val); void addAtIndex(int index, int val); void deleteAtIndex(int index); }; ``` ### Examples **Example 1:** ```text MyLinkedList(); addAtHead(4); addAtTail(9); addAtIndex(1,6); get(1); deleteAtIndex(0); get(0) Query returns: [6,6] Explanation: [4,6,9] becomes [6,9]. ``` **Example 2:** ```text MyLinkedList(); addAtIndex(2,7); get(0); addAtIndex(0,5); deleteAtIndex(3); get(0) Query returns: [-1,5] Explanation: Invalid insertion and deletion leave the list unchanged. ``` ### Constraints - Both index and val are in [0,1000]. - Across get, addAtHead, addAtTail, addAtIndex and deleteAtIndex, there are at most 2000 calls. - Implement links yourself; the built-in LinkedList library is excluded. ### Contract considerations Retain the original six-method stateful interface and avoid a library LinkedList. ```hint Read the boundary condition What predecessor node makes both insertion and deletion uniform? ``` ### Boundary reminders Retain the original six-method stateful interface and avoid a library LinkedList.

Overview: Maintain a List with Indexed Insertions and Deletions. Implement MyLinkedList using your own singly or doubly linked nodes, indexed from zero.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement MyLinkedList using your own singly or doubly linked nodes, indexed from zero. get(index) returns the value or -1 for an invalid index. addAtHead and addAtTail insert at either end. addAtIndex(index,val) inserts before that index, appends when index equals the size, and does nothing when index exceeds it. deleteAtIndex removes a valid indexed node and otherwise does nothing.

class MyLinkedList {
public:
    MyLinkedList();
    int get(int index);
    void addAtHead(int val);
    void addAtTail(int val);
    void addAtIndex(int index, int val);
    void deleteAtIndex(int index);
};

Examples

Example 1:

MyLinkedList(); addAtHead(4); addAtTail(9); addAtIndex(1,6); get(1); deleteAtIndex(0); get(0)
Query returns: [6,6]
Explanation: [4,6,9] becomes [6,9].

Example 2:

MyLinkedList(); addAtIndex(2,7); get(0); addAtIndex(0,5); deleteAtIndex(3); get(0)
Query returns: [-1,5]
Explanation: Invalid insertion and deletion leave the list unchanged.

Constraints

  • Both index and val are in [0,1000].
  • Across get, addAtHead, addAtTail, addAtIndex and deleteAtIndex, there are at most 2000 calls.
  • Implement links yourself; the built-in LinkedList library is excluded.

Contract considerations

Retain the original six-method stateful interface and avoid a library LinkedList.

Boundary reminders

Retain the original six-method stateful interface and avoid a library LinkedList.