{ "data": { "question": { "questionId": "1337", "questionFrontendId": "1206", "boundTopicId": null, "title": "Design Skiplist", "titleSlug": "design-skiplist", "content": "

Design a Skiplist without using any built-in libraries.

\n\n

A skiplist is a data structure that takes O(log(n)) time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists is just simple linked lists.

\n\n

For example, we have a Skiplist containing [30,40,50,60,70,90] and we want to add 80 and 45 into it. The Skiplist works this way:

\n\n

\"\"
\nArtyom Kalinin [CC BY-SA 3.0], via Wikimedia Commons

\n\n

You can see there are many layers in the Skiplist. Each layer is a sorted linked list. With the help of the top layers, add, erase and search can be faster than O(n). It can be proven that the average time complexity for each operation is O(log(n)) and space complexity is O(n).

\n\n

See more about Skiplist: https://en.wikipedia.org/wiki/Skip_list

\n\n

Implement the Skiplist class:

\n\n\n\n

Note that duplicates may exist in the Skiplist, your code needs to handle this situation.

\n\n

 

\n

Example 1:

\n\n
\nInput\n["Skiplist", "add", "add", "add", "search", "add", "search", "erase", "erase", "search"]\n[[], [1], [2], [3], [0], [4], [1], [0], [1], [1]]\nOutput\n[null, null, null, null, false, null, true, false, true, false]\n\nExplanation\nSkiplist skiplist = new Skiplist();\nskiplist.add(1);\nskiplist.add(2);\nskiplist.add(3);\nskiplist.search(0); // return False\nskiplist.add(4);\nskiplist.search(1); // return True\nskiplist.erase(0);  // return False, 0 is not in skiplist.\nskiplist.erase(1);  // return True\nskiplist.search(1); // return False, 1 has already been erased.
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Hard", "likes": 636, "dislikes": 78, "isLiked": null, "similarQuestions": "[{\"title\": \"Design HashSet\", \"titleSlug\": \"design-hashset\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Design HashMap\", \"titleSlug\": \"design-hashmap\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Design Linked List\", \"titleSlug\": \"design-linked-list\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "exampleTestcases": "[\"Skiplist\",\"add\",\"add\",\"add\",\"search\",\"add\",\"search\",\"erase\",\"erase\",\"search\"]\n[[],[1],[2],[3],[0],[4],[1],[0],[1],[1]]", "categoryTitle": "Algorithms", "contributors": [], "topicTags": [ { "name": "Linked List", "slug": "linked-list", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Design", "slug": "design", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "C++", "langSlug": "cpp", "code": "class Skiplist {\npublic:\n Skiplist() {\n \n }\n \n bool search(int target) {\n \n }\n \n void add(int num) {\n \n }\n \n bool erase(int num) {\n \n }\n};\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * Skiplist* obj = new Skiplist();\n * bool param_1 = obj->search(target);\n * obj->add(num);\n * bool param_3 = obj->erase(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Java", "langSlug": "java", "code": "class Skiplist {\n\n public Skiplist() {\n \n }\n \n public boolean search(int target) {\n \n }\n \n public void add(int num) {\n \n }\n \n public boolean erase(int num) {\n \n }\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * Skiplist obj = new Skiplist();\n * boolean param_1 = obj.search(target);\n * obj.add(num);\n * boolean param_3 = obj.erase(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Python", "langSlug": "python", "code": "class Skiplist(object):\n\n def __init__(self):\n \n\n def search(self, target):\n \"\"\"\n :type target: int\n :rtype: bool\n \"\"\"\n \n\n def add(self, num):\n \"\"\"\n :type num: int\n :rtype: None\n \"\"\"\n \n\n def erase(self, num):\n \"\"\"\n :type num: int\n :rtype: bool\n \"\"\"\n \n\n\n# Your Skiplist object will be instantiated and called as such:\n# obj = Skiplist()\n# param_1 = obj.search(target)\n# obj.add(num)\n# param_3 = obj.erase(num)", "__typename": "CodeSnippetNode" }, { "lang": "Python3", "langSlug": "python3", "code": "class Skiplist:\n\n def __init__(self):\n \n\n def search(self, target: int) -> bool:\n \n\n def add(self, num: int) -> None:\n \n\n def erase(self, num: int) -> bool:\n \n\n\n# Your Skiplist object will be instantiated and called as such:\n# obj = Skiplist()\n# param_1 = obj.search(target)\n# obj.add(num)\n# param_3 = obj.erase(num)", "__typename": "CodeSnippetNode" }, { "lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n \n} Skiplist;\n\n\nSkiplist* skiplistCreate() {\n \n}\n\nbool skiplistSearch(Skiplist* obj, int target) {\n \n}\n\nvoid skiplistAdd(Skiplist* obj, int num) {\n \n}\n\nbool skiplistErase(Skiplist* obj, int num) {\n \n}\n\nvoid skiplistFree(Skiplist* obj) {\n \n}\n\n/**\n * Your Skiplist struct will be instantiated and called as such:\n * Skiplist* obj = skiplistCreate();\n * bool param_1 = skiplistSearch(obj, target);\n \n * skiplistAdd(obj, num);\n \n * bool param_3 = skiplistErase(obj, num);\n \n * skiplistFree(obj);\n*/", "__typename": "CodeSnippetNode" }, { "lang": "C#", "langSlug": "csharp", "code": "public class Skiplist {\n\n public Skiplist() {\n \n }\n \n public bool Search(int target) {\n \n }\n \n public void Add(int num) {\n \n }\n \n public bool Erase(int num) {\n \n }\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * Skiplist obj = new Skiplist();\n * bool param_1 = obj.Search(target);\n * obj.Add(num);\n * bool param_3 = obj.Erase(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "JavaScript", "langSlug": "javascript", "code": "\nvar Skiplist = function() {\n \n};\n\n/** \n * @param {number} target\n * @return {boolean}\n */\nSkiplist.prototype.search = function(target) {\n \n};\n\n/** \n * @param {number} num\n * @return {void}\n */\nSkiplist.prototype.add = function(num) {\n \n};\n\n/** \n * @param {number} num\n * @return {boolean}\n */\nSkiplist.prototype.erase = function(num) {\n \n};\n\n/** \n * Your Skiplist object will be instantiated and called as such:\n * var obj = new Skiplist()\n * var param_1 = obj.search(target)\n * obj.add(num)\n * var param_3 = obj.erase(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "class Skiplist {\n constructor() {\n \n }\n\n search(target: number): boolean {\n \n }\n\n add(num: number): void {\n \n }\n\n erase(num: number): boolean {\n \n }\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * var obj = new Skiplist()\n * var param_1 = obj.search(target)\n * obj.add(num)\n * var param_3 = obj.erase(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "PHP", "langSlug": "php", "code": "class Skiplist {\n /**\n */\n function __construct() {\n \n }\n \n /**\n * @param Integer $target\n * @return Boolean\n */\n function search($target) {\n \n }\n \n /**\n * @param Integer $num\n * @return NULL\n */\n function add($num) {\n \n }\n \n /**\n * @param Integer $num\n * @return Boolean\n */\n function erase($num) {\n \n }\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * $obj = Skiplist();\n * $ret_1 = $obj->search($target);\n * $obj->add($num);\n * $ret_3 = $obj->erase($num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Swift", "langSlug": "swift", "code": "\nclass Skiplist {\n\n init() {\n \n }\n \n func search(_ target: Int) -> Bool {\n \n }\n \n func add(_ num: Int) {\n \n }\n \n func erase(_ num: Int) -> Bool {\n \n }\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * let obj = Skiplist()\n * let ret_1: Bool = obj.search(target)\n * obj.add(num)\n * let ret_3: Bool = obj.erase(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Kotlin", "langSlug": "kotlin", "code": "class Skiplist() {\n\n fun search(target: Int): Boolean {\n \n }\n\n fun add(num: Int) {\n \n }\n\n fun erase(num: Int): Boolean {\n \n }\n\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * var obj = Skiplist()\n * var param_1 = obj.search(target)\n * obj.add(num)\n * var param_3 = obj.erase(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Dart", "langSlug": "dart", "code": "class Skiplist {\n\n Skiplist() {\n \n }\n \n bool search(int target) {\n \n }\n \n void add(int num) {\n \n }\n \n bool erase(int num) {\n \n }\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * Skiplist obj = Skiplist();\n * bool param1 = obj.search(target);\n * obj.add(num);\n * bool param3 = obj.erase(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Go", "langSlug": "golang", "code": "type Skiplist struct {\n \n}\n\n\nfunc Constructor() Skiplist {\n \n}\n\n\nfunc (this *Skiplist) Search(target int) bool {\n \n}\n\n\nfunc (this *Skiplist) Add(num int) {\n \n}\n\n\nfunc (this *Skiplist) Erase(num int) bool {\n \n}\n\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * obj := Constructor();\n * param_1 := obj.Search(target);\n * obj.Add(num);\n * param_3 := obj.Erase(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Ruby", "langSlug": "ruby", "code": "class Skiplist\n def initialize()\n \n end\n\n\n=begin\n :type target: Integer\n :rtype: Boolean\n=end\n def search(target)\n \n end\n\n\n=begin\n :type num: Integer\n :rtype: Void\n=end\n def add(num)\n \n end\n\n\n=begin\n :type num: Integer\n :rtype: Boolean\n=end\n def erase(num)\n \n end\n\n\nend\n\n# Your Skiplist object will be instantiated and called as such:\n# obj = Skiplist.new()\n# param_1 = obj.search(target)\n# obj.add(num)\n# param_3 = obj.erase(num)", "__typename": "CodeSnippetNode" }, { "lang": "Scala", "langSlug": "scala", "code": "class Skiplist() {\n\n def search(target: Int): Boolean = {\n \n }\n\n def add(num: Int) {\n \n }\n\n def erase(num: Int): Boolean = {\n \n }\n\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * var obj = new Skiplist()\n * var param_1 = obj.search(target)\n * obj.add(num)\n * var param_3 = obj.erase(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Rust", "langSlug": "rust", "code": "struct Skiplist {\n\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl Skiplist {\n\n fn new() -> Self {\n \n }\n \n fn search(&self, target: i32) -> bool {\n \n }\n \n fn add(&self, num: i32) {\n \n }\n \n fn erase(&self, num: i32) -> bool {\n \n }\n}\n\n/**\n * Your Skiplist object will be instantiated and called as such:\n * let obj = Skiplist::new();\n * let ret_1: bool = obj.search(target);\n * obj.add(num);\n * let ret_3: bool = obj.erase(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Racket", "langSlug": "racket", "code": "(define skiplist%\n (class object%\n (super-new)\n \n (init-field)\n \n ; search : exact-integer? -> boolean?\n (define/public (search target)\n )\n ; add : exact-integer? -> void?\n (define/public (add num)\n )\n ; erase : exact-integer? -> boolean?\n (define/public (erase num)\n )))\n\n;; Your skiplist% object will be instantiated and called as such:\n;; (define obj (new skiplist%))\n;; (define param_1 (send obj search target))\n;; (send obj add num)\n;; (define param_3 (send obj erase num))", "__typename": "CodeSnippetNode" }, { "lang": "Erlang", "langSlug": "erlang", "code": "-spec skiplist_init_() -> any().\nskiplist_init_() ->\n .\n\n-spec skiplist_search(Target :: integer()) -> boolean().\nskiplist_search(Target) ->\n .\n\n-spec skiplist_add(Num :: integer()) -> any().\nskiplist_add(Num) ->\n .\n\n-spec skiplist_erase(Num :: integer()) -> boolean().\nskiplist_erase(Num) ->\n .\n\n\n%% Your functions will be called as such:\n%% skiplist_init_(),\n%% Param_1 = skiplist_search(Target),\n%% skiplist_add(Num),\n%% Param_3 = skiplist_erase(Num),\n\n%% skiplist_init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode" }, { "lang": "Elixir", "langSlug": "elixir", "code": "defmodule Skiplist do\n @spec init_() :: any\n def init_() do\n \n end\n\n @spec search(target :: integer) :: boolean\n def search(target) do\n \n end\n\n @spec add(num :: integer) :: any\n def add(num) do\n \n end\n\n @spec erase(num :: integer) :: boolean\n def erase(num) do\n \n end\nend\n\n# Your functions will be called as such:\n# Skiplist.init_()\n# param_1 = Skiplist.search(target)\n# Skiplist.add(num)\n# param_3 = Skiplist.erase(num)\n\n# Skiplist.init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"20K\", \"totalSubmission\": \"32.9K\", \"totalAcceptedRaw\": 19961, \"totalSubmissionRaw\": 32942, \"acRate\": \"60.6%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "[\"Skiplist\",\"add\",\"add\",\"add\",\"search\",\"add\",\"search\",\"erase\",\"erase\",\"search\"]\n[[],[1],[2],[3],[0],[4],[1],[0],[1],[1]]", "metaData": "{\n \"classname\": \"Skiplist\",\n \"maxbytesperline\": 160000,\n \"constructor\": {\n \"params\": []\n },\n \"methods\": [\n {\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"target\"\n }\n ],\n \"name\": \"search\",\n \"return\": {\n \"type\": \"boolean\"\n }\n },\n {\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"num\"\n }\n ],\n \"name\": \"add\",\n \"return\": {\n \"type\": \"void\"\n }\n },\n {\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"num\"\n }\n ],\n \"name\": \"erase\",\n \"return\": {\n \"type\": \"boolean\"\n }\n }\n ],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"systemdesign\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": true, "envInfo": "{\"cpp\": [\"C++\", \"

Compiled with clang 11 using the latest C++ 20 standard.

\\r\\n\\r\\n

Your code is compiled with level two optimization (-O2). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\"], \"java\": [\"Java\", \"

OpenJDK 17. Java 8 features such as lambda expressions and stream API can be used.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n

Includes Pair class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.

\"], \"python\": [\"Python\", \"

Python 2.7.12.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\\r\\n\\r\\n

Note that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.

\"], \"c\": [\"C\", \"

Compiled with gcc 8.2 using the gnu11 standard.

\\r\\n\\r\\n

Your code is compiled with level one optimization (-O1). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n\\r\\n

For hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:

\\r\\n\\r\\n

1. Adding an item to a hash.\\r\\n

\\r\\nstruct hash_entry {\\r\\n    int id;            /* we'll use this field as the key */\\r\\n    char name[10];\\r\\n    UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n    HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

2. Looking up an item in a hash:\\r\\n

\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n    struct hash_entry *s;\\r\\n    HASH_FIND_INT(users, &user_id, s);\\r\\n    return s;\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

3. Deleting an item in a hash:\\r\\n

\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n    HASH_DEL(users, user);  \\r\\n}\\r\\n
\\r\\n

\"], \"csharp\": [\"C#\", \"

C# 10 with .NET 6 runtime

\"], \"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"ruby\": [\"Ruby\", \"

Ruby 3.1

\\r\\n\\r\\n

Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms

\"], \"swift\": [\"Swift\", \"

Swift 5.5.2.

\"], \"golang\": [\"Go\", \"

Go 1.21

\\r\\n

Support https://godoc.org/github.com/emirpasic/gods@v1.18.1 library.

\"], \"python3\": [\"Python3\", \"

Python 3.10.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\"], \"scala\": [\"Scala\", \"

Scala 2.13.7.

\"], \"kotlin\": [\"Kotlin\", \"

Kotlin 1.9.0.

\"], \"rust\": [\"Rust\", \"

Rust 1.58.1

\\r\\n\\r\\n

Supports rand v0.6\\u00a0from crates.io

\"], \"php\": [\"PHP\", \"

PHP 8.1.

\\r\\n

With bcmath module

\"], \"typescript\": [\"Typescript\", \"

TypeScript 5.1.6, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"], \"racket\": [\"Racket\", \"

Run with Racket 8.3.

\"], \"erlang\": [\"Erlang\", \"Erlang/OTP 25.0\"], \"elixir\": [\"Elixir\", \"Elixir 1.13.4 with Erlang/OTP 25.0\"], \"dart\": [\"Dart\", \"

Dart 2.17.3

\\r\\n\\r\\n

Your code will be run directly without compiling

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }