Problem ยท Hash Table

Count User Logins ๐ŸŒฟ (Singapore)

Learn this problem
โ— MediumPaypalINTERNOA

Problem statement

A company wants to track the usage of its mobile app by recording users' login times and dates. The company stores the login information in a 2D array of strings, logs, which contains data in the format ["/username<user_id>","login_time","login_date"].

They need a function to process the logs and output a 2D array of strings, sorted lexicographically, that displays the number of times each user logs in per day in the format ["/username<user_id>","login_date","login_count"]. It must filter invalid data in the input array rather than write it to the output array.

Users should be sorted in lexicographic order based on their user_id. Each user's information should be sorted by the login date in ascending order. The date and time are provided in YYYY-MM-DD and HH:MM:SS format, and the username has the format "userX" where X is an integer.

Function

countUserLogins(logs: String[][]) โ†’ String[][]

Complete the function countUserLogins in the editor.

countUserLogins has the following parameter:

  1. String[][] logs: a 2D array of strings containing the login data

Returns

String[][]: a 2D array of strings containing the user login counts per day, sorted lexicographically

Examples

Example 1

logs = [["user1","09:00:00","2021-01-01"],["user1","13:00:00","2021-01-01"],["user2","14:00:00","2021-01-01"],["user1","20:00:00","2021-01-02"],["user2","21:00:00","2021-01-01"]]return = [["user1","2021-01-01","2"],["user1","2021-01-02","1"],["user2","2021-01-01","2"]]
Example 1 illustration
The function should output the login counts for each user per day, sorted lexicographically, resulting in a 2-d array of strings in the format [["user1","2021-01-01","3"],["user1","2021-01-02","1"],["user2","2021-01-01","1"]].

Example 2

logs = [["user1","09:00:00","2021-01-01"],["user1","13:00:00","2021-01-01"],["user2","14:00:00","2021-01-01"],["user1","20:00:00","2021-01-01"],["user2","21:00:00","2021-01-01"], ["user3","25:00:00","2021-01-01"], ["user4","22:00:00","2021-02-29"]]return = [["user1","2021-01-01","3"],["user2","2021-01-01","2"]]
  • user1 logs into the system 3 times on 2021-01-01
  • user2 logs into the system 3 times on 2021-01-01
  • entry 6 in the logs has an invalid time, 25:00:00.
  • entry 7 in the logs has an invalid data, 2021-02-29.
  • Constraints

  • 1 โ‰ค n (size of logs) โ‰ค 10^5
  • 2000 โ‰ค YYYY โ‰ค 3000
  • 0 โ‰ค MM, DD, HH, SS โ‰ค 99
  • YYYY, MM, DD, HH, and SS are all integers.
  • All usernames are of the format "user" appended with some integer (user_id).
  • More Paypal problems

    drafts saved locally
    public String[][] countUserLogins(String[][] logs) {
        // write your code here
    }
    
    logs[["user1","09:00:00","2021-01-01"],["user1","13:00:00","2021-01-01"],["user2","14:00:00","2021-01-01"],["user1","20:00:00","2021-01-02"],["user2","21:00:00","2021-01-01"]]
    expected[["user1", "2021-01-01", "2", "user1", "2021-01-02", "1", "user2", "2021-01-01", "2"]]
    checking account