178. Design Digital Wellbeing System to Track App Screen Usage

Asked in

Design a Digital Wellbeing System to Track Screen Usage of Apps
You need to design a DigitalWellbeingSystem that keeps track of screen usage for different applications.

The system receives usage records for apps. Each usage record contains an app name, a day number, and the amount of screen time used on that day.

The system should allow querying usage statistics for applications.

The system only stores data for the most recent 7 days based on the latest day seen by the system. Older data must be removed automatically.

Day values passed to addUsage will be monotonic non-decreasing across all addUsage calls on the same object.

Method Signature

Initialize

DigitalWellbeingSystem()
  • Creates a new empty object of the digital wellbeing system.

Add Usage

void addUsage(String appName, int day, int minutes)
  • appName is the name of the application.
  • day is an integer representing the day number.
  • minutes is the screen time used by the application on that day.
  • Across all calls to addUsage on the same object, day values will be monotonic non-decreasing.
  • If the same app receives multiple usage records for the same day, add all minutes together.
  • After adding usage, keep only data from the most recent 7 days based on the latest day seen so far.

Get App Usage

int getAppUsage(String appName, int day)
  • Returns the total usage in minutes for appName on the given day.
  • Returns 0 if the app has no stored usage for that day.
  • Returns 0 if the requested day is outside the stored last 7 days.

Get Total Usage

int getTotalUsage(int day)
  • Returns the total screen time in minutes across all apps for the given day.
  • Returns 0 if there is no stored usage for that day.
  • Returns 0 if the requested day is outside the stored last 7 days.

Get Usage Statistics

List<String> getUsageStatistics()
  • Returns usage statistics for all stored app-day pairs.
  • Each string must be formatted as "day,appName,minutes".
  • The result must be sorted by day in increasing order.
  • If two records have the same day, sort them by appName in lexicographical order.
  • Lexicographical order is case-sensitive.
  • Only data from the stored last 7 days should be returned.

Retention Rule

Let latestDay be the greatest day value passed to addUsage so far for the current object. The system should only keep usage data where:
  • latestDay - 6 ≤ day ≤ latestDay

Day Order Rule

Day values in addUsage calls are monotonic non-decreasing. This means each new addUsage call will have a day value greater than or equal to the previous addUsage call's day value for the same object. Query methods may request any day value.

Constraints

  • 1 ≤ appName.length() ≤ 50
  • appName contains lowercase letters, uppercase letters, digits, spaces, hyphens, or underscores.
  • 1 ≤ day ≤ 10^9
  • 1 ≤ minutes ≤ 1440
  • At most 10^5 calls will be made in total.
  • Parameter values will never be null.

Examples

Example 1

DigitalWellbeingSystem system = new DigitalWellbeingSystem()
system.addUsage(appName = "ChatApp", day = 10, minutes = 40)
system.addUsage(appName = "VideoApp", day = 10, minutes = 90)
system.addUsage(appName = "ChatApp", day = 10, minutes = 15)
system.getAppUsage(appName = "ChatApp", day = 10)
Output: 55
system.getTotalUsage(day = 10)
Output: 145
system.getUsageStatistics()
Output: ["10,ChatApp,55", "10,VideoApp,90"]

Example 2

DigitalWellbeingSystem system = new DigitalWellbeingSystem()
system.addUsage(appName = "Browser", day = 3, minutes = 25)
system.addUsage(appName = "Music", day = 4, minutes = 70)
system.addUsage(appName = "Browser", day = 9, minutes = 35)
Since the latest day is 9, only days from 3 to 9 are stored.
system.getAppUsage(appName = "Browser", day = 3)
Output: 25
system.addUsage(appName = "Game", day = 10, minutes = 120)
Since the latest day is now 10, only days from 4 to 10 are stored.
system.getAppUsage(appName = "Browser", day = 3)
Output: 0
system.getUsageStatistics()
Output: ["4,Music,70", "9,Browser,35", "10,Game,120"]

Example 3

DigitalWellbeingSystem system = new DigitalWellbeingSystem()
system.addUsage(appName = "Mail", day = 20, minutes = 45)
system.addUsage(appName = "Mail", day = 21, minutes = 30)
system.addUsage(appName = "Calendar", day = 21, minutes = 15)
system.getAppUsage(appName = "Mail", day = 20)
Output: 45
system.getTotalUsage(day = 21)
Output: 45
system.getUsageStatistics()
Output: ["20,Mail,45", "21,Calendar,15", "21,Mail,30"]

Example 4

DigitalWellbeingSystem system = new DigitalWellbeingSystem()
system.addUsage(appName = "A", day = 1, minutes = 10)
system.addUsage(appName = "B", day = 2, minutes = 20)
system.addUsage(appName = "C", day = 7, minutes = 30)
system.addUsage(appName = "D", day = 8, minutes = 40)
Since the latest day is 8, only days from 2 to 8 are stored.
system.getTotalUsage(day = 1)
Output: 0
system.getTotalUsage(day = 2)
Output: 20
system.getUsageStatistics()
Output: ["2,B,20", "7,C,30", "8,D,40"]




Please use Laptop/Desktop or any other large screen to add/edit code.