समस्या
The singleton pattern makes sure a class has exactly one instance that everyone shares, a common choice for application settings.
Simulate one shared configuration that starts empty. Process the operations in order:
set <key> <value>: store the value for the key (replacing any old value).get <key>: read the key's current value, ormissingif it was never set.
Because every caller uses the same instance, a value set by one operation is visible to every later get.
Implement shared_configuration(operations).
Input
operations: Strings in the two forms above. Keys and values contain no spaces.
Output
Return the results of the get operations, in order.
सीमाएँ
1 ≤ number of operations ≤ 10,000
Keys and values have 1 to 20 characters
उदाहरण
इनपुट:
operations = ["set theme dark", "get theme", "get language"]
अपेक्षित आउटपुट:
expected = ["dark", "missing"]
इनपुट:
std::vector<std::string> operations = {"set theme dark", "get theme", "get language"};
अपेक्षित आउटपुट:
std::vector<std::string> expected = {"dark", "missing"};
इनपुट:
string[] operations = new string[] {"set theme dark", "get theme", "get language"};
अपेक्षित आउटपुट:
string[] expected = new string[] {"dark", "missing"};
व्याख्या
theme was set to dark; language was never set, so it is missing: ["dark", "missing"].
इनपुट:
operations = ["set lang en", "set lang vi", "get lang"]
अपेक्षित आउटपुट:
expected = ["vi"]
इनपुट:
std::vector<std::string> operations = {"set lang en", "set lang vi", "get lang"};
अपेक्षित आउटपुट:
std::vector<std::string> expected = {"vi"};
इनपुट:
string[] operations = new string[] {"set lang en", "set lang vi", "get lang"};
अपेक्षित आउटपुट:
string[] expected = new string[] {"vi"};
व्याख्या
The second set replaces the first value in the same shared instance: ["vi"].
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
All operations read and write the same single object.
संकेत 2
A dictionary from key to value is enough to hold the settings.
संकेत 3
A key that was never set must return the text missing.
अपेक्षित जटिलता
O(number of operations) time, O(number of keys) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।