{ "data": { "question": { "questionId": "2434", "questionFrontendId": "2349", "boundTopicId": null, "title": "Design a Number Container System", "titleSlug": "design-a-number-container-system", "content": "

Design a number container system that can do the following:

\n\n\n\n

Implement the NumberContainers class:

\n\n\n\n

 

\n

Example 1:

\n\n
\nInput\n["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]\n[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]\nOutput\n[null, -1, null, null, null, null, 1, null, 2]\n\nExplanation\nNumberContainers nc = new NumberContainers();\nnc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.\nnc.change(2, 10); // Your container at index 2 will be filled with number 10.\nnc.change(1, 10); // Your container at index 1 will be filled with number 10.\nnc.change(3, 10); // Your container at index 3 will be filled with number 10.\nnc.change(5, 10); // Your container at index 5 will be filled with number 10.\nnc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.\nnc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20. \nnc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 185, "dislikes": 13, "isLiked": null, "similarQuestions": "[{\"title\": \"Seat Reservation Manager\", \"titleSlug\": \"seat-reservation-manager\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Design a Food Rating System\", \"titleSlug\": \"design-a-food-rating-system\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "exampleTestcases": "[\"NumberContainers\",\"find\",\"change\",\"change\",\"change\",\"change\",\"find\",\"change\",\"find\"]\n[[],[10],[2,10],[1,10],[3,10],[5,10],[10],[1,20],[10]]", "categoryTitle": "Algorithms", "contributors": [], "topicTags": [ { "name": "Hash Table", "slug": "hash-table", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Design", "slug": "design", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Ordered Set", "slug": "ordered-set", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "C++", "langSlug": "cpp", "code": "class NumberContainers {\npublic:\n NumberContainers() {\n \n }\n \n void change(int index, int number) {\n \n }\n \n int find(int number) {\n \n }\n};\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * NumberContainers* obj = new NumberContainers();\n * obj->change(index,number);\n * int param_2 = obj->find(number);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Java", "langSlug": "java", "code": "class NumberContainers {\n\n public NumberContainers() {\n \n }\n \n public void change(int index, int number) {\n \n }\n \n public int find(int number) {\n \n }\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * NumberContainers obj = new NumberContainers();\n * obj.change(index,number);\n * int param_2 = obj.find(number);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Python", "langSlug": "python", "code": "class NumberContainers(object):\n\n def __init__(self):\n \n\n def change(self, index, number):\n \"\"\"\n :type index: int\n :type number: int\n :rtype: None\n \"\"\"\n \n\n def find(self, number):\n \"\"\"\n :type number: int\n :rtype: int\n \"\"\"\n \n\n\n# Your NumberContainers object will be instantiated and called as such:\n# obj = NumberContainers()\n# obj.change(index,number)\n# param_2 = obj.find(number)", "__typename": "CodeSnippetNode" }, { "lang": "Python3", "langSlug": "python3", "code": "class NumberContainers:\n\n def __init__(self):\n \n\n def change(self, index: int, number: int) -> None:\n \n\n def find(self, number: int) -> int:\n \n\n\n# Your NumberContainers object will be instantiated and called as such:\n# obj = NumberContainers()\n# obj.change(index,number)\n# param_2 = obj.find(number)", "__typename": "CodeSnippetNode" }, { "lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n \n} NumberContainers;\n\n\nNumberContainers* numberContainersCreate() {\n \n}\n\nvoid numberContainersChange(NumberContainers* obj, int index, int number) {\n \n}\n\nint numberContainersFind(NumberContainers* obj, int number) {\n \n}\n\nvoid numberContainersFree(NumberContainers* obj) {\n \n}\n\n/**\n * Your NumberContainers struct will be instantiated and called as such:\n * NumberContainers* obj = numberContainersCreate();\n * numberContainersChange(obj, index, number);\n \n * int param_2 = numberContainersFind(obj, number);\n \n * numberContainersFree(obj);\n*/", "__typename": "CodeSnippetNode" }, { "lang": "C#", "langSlug": "csharp", "code": "public class NumberContainers {\n\n public NumberContainers() {\n \n }\n \n public void Change(int index, int number) {\n \n }\n \n public int Find(int number) {\n \n }\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * NumberContainers obj = new NumberContainers();\n * obj.Change(index,number);\n * int param_2 = obj.Find(number);\n */", "__typename": "CodeSnippetNode" }, { "lang": "JavaScript", "langSlug": "javascript", "code": "\nvar NumberContainers = function() {\n \n};\n\n/** \n * @param {number} index \n * @param {number} number\n * @return {void}\n */\nNumberContainers.prototype.change = function(index, number) {\n \n};\n\n/** \n * @param {number} number\n * @return {number}\n */\nNumberContainers.prototype.find = function(number) {\n \n};\n\n/** \n * Your NumberContainers object will be instantiated and called as such:\n * var obj = new NumberContainers()\n * obj.change(index,number)\n * var param_2 = obj.find(number)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Ruby", "langSlug": "ruby", "code": "class NumberContainers\n def initialize()\n \n end\n\n\n=begin\n :type index: Integer\n :type number: Integer\n :rtype: Void\n=end\n def change(index, number)\n \n end\n\n\n=begin\n :type number: Integer\n :rtype: Integer\n=end\n def find(number)\n \n end\n\n\nend\n\n# Your NumberContainers object will be instantiated and called as such:\n# obj = NumberContainers.new()\n# obj.change(index, number)\n# param_2 = obj.find(number)", "__typename": "CodeSnippetNode" }, { "lang": "Swift", "langSlug": "swift", "code": "\nclass NumberContainers {\n\n init() {\n \n }\n \n func change(_ index: Int, _ number: Int) {\n \n }\n \n func find(_ number: Int) -> Int {\n \n }\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * let obj = NumberContainers()\n * obj.change(index, number)\n * let ret_2: Int = obj.find(number)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Go", "langSlug": "golang", "code": "type NumberContainers struct {\n \n}\n\n\nfunc Constructor() NumberContainers {\n \n}\n\n\nfunc (this *NumberContainers) Change(index int, number int) {\n \n}\n\n\nfunc (this *NumberContainers) Find(number int) int {\n \n}\n\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * obj := Constructor();\n * obj.Change(index,number);\n * param_2 := obj.Find(number);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Scala", "langSlug": "scala", "code": "class NumberContainers() {\n\n def change(index: Int, number: Int) {\n \n }\n\n def find(number: Int): Int = {\n \n }\n\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * var obj = new NumberContainers()\n * obj.change(index,number)\n * var param_2 = obj.find(number)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Kotlin", "langSlug": "kotlin", "code": "class NumberContainers() {\n\n fun change(index: Int, number: Int) {\n \n }\n\n fun find(number: Int): Int {\n \n }\n\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * var obj = NumberContainers()\n * obj.change(index,number)\n * var param_2 = obj.find(number)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Rust", "langSlug": "rust", "code": "struct NumberContainers {\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 NumberContainers {\n\n fn new() -> Self {\n \n }\n \n fn change(&self, index: i32, number: i32) {\n \n }\n \n fn find(&self, number: i32) -> i32 {\n \n }\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * let obj = NumberContainers::new();\n * obj.change(index, number);\n * let ret_2: i32 = obj.find(number);\n */", "__typename": "CodeSnippetNode" }, { "lang": "PHP", "langSlug": "php", "code": "class NumberContainers {\n /**\n */\n function __construct() {\n \n }\n \n /**\n * @param Integer $index\n * @param Integer $number\n * @return NULL\n */\n function change($index, $number) {\n \n }\n \n /**\n * @param Integer $number\n * @return Integer\n */\n function find($number) {\n \n }\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * $obj = NumberContainers();\n * $obj->change($index, $number);\n * $ret_2 = $obj->find($number);\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "class NumberContainers {\n constructor() {\n\n }\n\n change(index: number, number: number): void {\n\n }\n\n find(number: number): number {\n\n }\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * var obj = new NumberContainers()\n * obj.change(index,number)\n * var param_2 = obj.find(number)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Racket", "langSlug": "racket", "code": "(define number-containers%\n (class object%\n (super-new)\n \n (init-field)\n \n ; change : exact-integer? exact-integer? -> void?\n (define/public (change index number)\n\n )\n ; find : exact-integer? -> exact-integer?\n (define/public (find number)\n\n )))\n\n;; Your number-containers% object will be instantiated and called as such:\n;; (define obj (new number-containers%))\n;; (send obj change index number)\n;; (define param_2 (send obj find number))", "__typename": "CodeSnippetNode" }, { "lang": "Erlang", "langSlug": "erlang", "code": "-spec number_containers_init_() -> any().\nnumber_containers_init_() ->\n .\n\n-spec number_containers_change(Index :: integer(), Number :: integer()) -> any().\nnumber_containers_change(Index, Number) ->\n .\n\n-spec number_containers_find(Number :: integer()) -> integer().\nnumber_containers_find(Number) ->\n .\n\n\n%% Your functions will be called as such:\n%% number_containers_init_(),\n%% number_containers_change(Index, Number),\n%% Param_2 = number_containers_find(Number),\n\n%% number_containers_init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode" }, { "lang": "Elixir", "langSlug": "elixir", "code": "defmodule NumberContainers do\n @spec init_() :: any\n def init_() do\n\n end\n\n @spec change(index :: integer, number :: integer) :: any\n def change(index, number) do\n\n end\n\n @spec find(number :: integer) :: integer\n def find(number) do\n\n end\nend\n\n# Your functions will be called as such:\n# NumberContainers.init_()\n# NumberContainers.change(index, number)\n# param_2 = NumberContainers.find(number)\n\n# NumberContainers.init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode" }, { "lang": "Dart", "langSlug": "dart", "code": "class NumberContainers {\n\n NumberContainers() {\n\n }\n \n void change(int index, int number) {\n\n }\n \n int find(int number) {\n\n }\n}\n\n/**\n * Your NumberContainers object will be instantiated and called as such:\n * NumberContainers obj = NumberContainers();\n * obj.change(index,number);\n * int param2 = obj.find(number);\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"13.5K\", \"totalSubmission\": \"29.7K\", \"totalAcceptedRaw\": 13457, \"totalSubmissionRaw\": 29737, \"acRate\": \"45.3%\"}", "hints": [ "Use a hash table to efficiently map each number to all of its indices in the container and to map each index to their current number.", "In addition, you can use ordered set to store all of the indices for each number to solve the find method. Do not forget to update the ordered set according to the change method." ], "solution": null, "status": null, "sampleTestCase": "[\"NumberContainers\",\"find\",\"change\",\"change\",\"change\",\"change\",\"find\",\"change\",\"find\"]\n[[],[10],[2,10],[1,10],[3,10],[5,10],[10],[1,20],[10]]", "metaData": "{\n \"classname\": \"NumberContainers\",\n \"constructor\": {\n \"params\": []\n },\n \"methods\": [\n {\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"index\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"number\"\n }\n ],\n \"name\": \"change\",\n \"return\": {\n \"type\": \"void\"\n }\n },\n {\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"number\"\n }\n ],\n \"name\": \"find\",\n \"return\": {\n \"type\": \"integer\"\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++ 17 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 datastructures-js/priority-queue and 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.17.6.

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

Support https://godoc.org/github.com/emirpasic/gods 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.3.10.

\"], \"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 4.5.4, Node.js 16.13.2.

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

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

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

lodash.js library is included by default.

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

Run with Racket 8.3.

\"], \"erlang\": [\"Erlang\", \"Erlang/OTP 24.2\"], \"elixir\": [\"Elixir\", \"Elixir 1.13.0 with Erlang/OTP 24.2\"], \"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" } } }