This is a design round. You write the classes, and the interviewer checks that they behave.
Here you build Logger, the logger of an application. It drops unimportant lines and stores only the newest ones.
Each line has a level. "DEBUG" is the lowest level. Then come "INFO" and "WARN". "ERROR" is the highest.
Implement the class:
new Logger(level, capacity)makes a logger with the levellevel. It stores at mostcapacitylines.Log(level, message)gets a line. It returnstrueif the line is kept, andfalseif it is dropped.SetLevel(level)changes the logger's level. It returns nothing.Count(level)returns how many lines of this level were kept.Recent(n)returns the newestnstored lines.
A line is kept when its level is at or above the logger's level. A lower line is dropped. It leaves no trace.
A kept line is stored. The logger never stores more than capacity lines. When a new line would make one too many, the oldest stored line is forgotten.
A new level from SetLevel applies to the lines that come after it. Lines already stored stay stored.
Count counts the kept lines of exactly that level since the start. Forgotten lines still count.
Recent returns the lines oldest first. Each one is the level, one space and the message, as in "WARN diskFull". When fewer than n lines are stored, it returns all of them. It doesn't remove any line.
The calls run in order on one new logger. A call to Log writes True or False. A call to SetLevel writes nothing. A call to Count writes the number. A call to Recent writes how many lines it returns on one line. Then it writes each line on its own line.
Example 1:
Input:
new Logger("INFO", 3)
1. Log("DEBUG", "start")
2. Log("INFO", "ready")
3. Log("WARN", "slowDisk")
4. Log("ERROR", "crash")
5. Log("INFO", "retry")
6. Recent(2)
7. Recent(10)
8. Count("INFO")
9. Count("DEBUG")
Output:
False
True
True
True
True
2
ERROR crash
INFO retry
3
WARN slowDisk
ERROR crash
INFO retry
2
0
Explanation:
The level is INFO, so call 1 drops its DEBUG line.
Calls 2, 3 and 4 fill the 3 places.
Call 5 brings a fourth line.
So the oldest one, "INFO ready", is forgotten.
Call 6 returns the 2 newest lines, oldest first.
Call 7 asks for 10 lines, but only 3 are stored.
Two INFO lines were kept. The forgotten one still counts.
The dropped DEBUG line doesn't count.
Example 2:
Input:
new Logger("DEBUG", 5)
1. Log("DEBUG", "cacheMiss")
2. SetLevel("ERROR")
3. Log("WARN", "slowQuery")
4. Log("ERROR", "dbDown")
5. SetLevel("INFO")
6. Log("DEBUG", "cacheHit")
7. Log("INFO", "dbBack")
8. Recent(5)
9. Count("WARN")
Output:
True
False
True
False
True
3
DEBUG cacheMiss
ERROR dbDown
INFO dbBack
0
Explanation:
The level starts at DEBUG, so call 1 keeps its line.
Call 2 raises the level to ERROR.
So call 3 drops a WARN line.
Call 5 lowers the level to INFO.
Now DEBUG lines are dropped, and INFO lines are kept.
The DEBUG line from call 1 stays stored.
Call 9 writes 0, because the only WARN line was dropped.
Example 3:
Input:
new Logger("WARN", 1)
1. Recent(3)
2. Log("INFO", "hello")
3. Count("INFO")
4. Log("WARN", "diskFull")
5. Log("ERROR", "outOfMemory")
6. Recent(1)
7. Count("WARN")
Output:
0
False
0
True
True
1
ERROR outOfMemory
1
Explanation:
Nothing is stored yet, so call 1 writes 0 and no lines.
INFO is below WARN, so call 2 drops its line.
This logger stores only 1 line.
So call 5 forgets the WARN line from call 4.
That WARN line still counts in call 7.
Constraints:
- Every
levelis "DEBUG", "INFO", "WARN" or "ERROR". capacityis from 1 to 100,000.- A
messagehas 1 to 20 English letters and digits. - A
messagehas no spaces. nis from 1 to 100,000.- At most 100,000 calls follow the constructor.
- The sum of
nover all calls toRecentis at most 200,000.
Note:
Each call to Log, SetLevel or Count should run in O(1) time. A call to Recent should take O(n) time. Removing the oldest line from the front of a list would take O(capacity) time. Keep the lines in a ring of capacity slots instead.
Free plan: 30 tutor messages. PRO is unlimited.
