Einfach 0 gelöst 0% Erfolgsquote

One Shared Configuration

Simulate a singleton settings object: every part of the program reads and writes the same instance.

Das lernst du

  • Share one object across the whole program
  • Create an instance lazily on first use
  • Read and update settings through one place

Problem

Übersetzung anzeigen

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, or missing if 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.

Einschränkungen

1 ≤ number of operations ≤ 10,000

Keys and values have 1 to 20 characters

Beispiele

Beispiel 1
Eingabe:
operations = ["set theme dark", "get theme", "get language"]

Erwartete Ausgabe:
expected = ["dark", "missing"]
Eingabe:
std::vector<std::string> operations = {"set theme dark", "get theme", "get language"};

Erwartete Ausgabe:
std::vector<std::string> expected = {"dark", "missing"};
Eingabe:
string[] operations = new string[] {"set theme dark", "get theme", "get language"};

Erwartete Ausgabe:
string[] expected = new string[] {"dark", "missing"};

Erläuterung

theme was set to dark; language was never set, so it is missing: ["dark", "missing"].

Beispiel 2
Eingabe:
operations = ["set lang en", "set lang vi", "get lang"]

Erwartete Ausgabe:
expected = ["vi"]
Eingabe:
std::vector<std::string> operations = {"set lang en", "set lang vi", "get lang"};

Erwartete Ausgabe:
std::vector<std::string> expected = {"vi"};
Eingabe:
string[] operations = new string[] {"set lang en", "set lang vi", "get lang"};

Erwartete Ausgabe:
string[] expected = new string[] {"vi"};

Erläuterung

The second set replaces the first value in the same shared instance: ["vi"].

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

All operations read and write the same single object.

Hinweis 2

A dictionary from key to value is enough to hold the settings.

Hinweis 3

A key that was never set must return the text missing.

Erwartete Komplexität

O(number of operations) time, O(number of keys) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.