Start learning free

LESSON 322 OF 333 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Logger with Levels in Java

Practice O(1)

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 level level. It stores at most capacity lines.
  • log(level, message) gets a line. It returns true if the line is kept, and false if 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 newest n stored 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 level is "DEBUG", "INFO", "WARN" or "ERROR".
  • capacity is from 1 to 100,000.
  • A message has 1 to 20 English letters and digits.
  • A message has no spaces.
  • n is from 1 to 100,000.
  • At most 100,000 calls follow the constructor.
  • The sum of n over all calls to recent is 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.

STEP 1 OF 26
    Ctrl + Enter to check

    Logger Levels in Java

    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free