Resource Transition Probabilities
Updated
Problem statement
A website keeps an access log. Each entry of logs is [time, user, resource]: at time, user opened resource. The entries are in no particular order, and time is a whole number written as a string.
Turn the log into a transition graph:
- For each user, sort that user's entries by
timeas numbers, smallest first. Put the marker__START__before the first resource and the marker__END__after the last one. This is the user's path. - Every two neighbors
A,Bon a path are one step fromAtoB. Count the steps of all users together. - For each
Athat has outgoing steps, the probability of going toBis the number of steps fromAtoB, divided by the number of steps that leaveA.
Return one line per pair A, B with at least one step, written as "A -> B: P". P is the probability rounded to exactly two decimal places, with an exact half rounded up (for example 0.125 becomes 0.13). It always has one digit before the point, like 0.50 or 1.00.
Order the lines like this:
- Group the lines by
A. The__START__group comes first, then the resources in plain character order. - Inside a group, the line to
__END__comes first, then the other targets in plain character order.
__END__ never starts a line, because nothing comes after it.
Function
transitionGraph(logs: String[][]) → String[]Examples
Example 1
logs = [["200","user_1","resource_5"],["3","user_1","resource_1"],["620","user_1","resource_1"],["620","user_3","resource_1"],["34","user_6","resource_2"],["95","user_9","resource_1"],["416","user_6","resource_1"],["58523","user_3","resource_1"],["53760","user_3","resource_3"],["58522","user_22","resource_1"],["100","user_3","resource_6"],["400","user_6","resource_2"]]return = ["__START__ -> resource_1: 0.60","__START__ -> resource_2: 0.20","__START__ -> resource_6: 0.20","resource_1 -> __END__: 0.71","resource_1 -> resource_3: 0.14","resource_1 -> resource_5: 0.14","resource_2 -> resource_1: 0.50","resource_2 -> resource_2: 0.50","resource_3 -> resource_1: 1.00","resource_5 -> resource_1: 1.00","resource_6 -> resource_1: 1.00"]Sorted by time, the journeys are: user_1 visits resource_1, resource_5, resource_1; user_3 visits resource_6, resource_1, resource_3, resource_1; user_6 visits resource_2, resource_2, resource_1; user_9 and user_22 visit only resource_1. Three of the five journeys begin at resource_1, so that start step has probability 3/5 = 0.60. resource_1 is left 7 times: 5 times to __END__ (5/7 is about 0.714, shown as 0.71), once to resource_3 and once to resource_5. Times are compared as numbers, so "3" comes before "200".
Example 2
logs = [["357","user","resource_2"],["1262","user","resource_1"],["1462","user","resource_2"],["1060","user","resource_1"],["756","user","resource_3"],["1090","user","resource_3"]]return = ["__START__ -> resource_2: 1.00","resource_1 -> resource_2: 0.50","resource_1 -> resource_3: 0.50","resource_2 -> __END__: 0.50","resource_2 -> resource_3: 0.50","resource_3 -> resource_1: 1.00"]One user visits resource_2, resource_3, resource_1, resource_3, resource_1, resource_2 in time order. resource_1 is left twice, once to resource_3 and once to resource_2. The last visit is followed by __END__.
Example 3
logs = [["300","user_10","resource_5"]]return = ["__START__ -> resource_5: 1.00","resource_5 -> __END__: 1.00"]A single visit gives one step from __START__ and one step to __END__.
Example 4
logs = [["1","user_96","resource_5"],["1","user_10","resource_5"],["301","user_11","resource_5"],["301","user_12","resource_5"],["603","user_12","resource_5"],["1603","user_12","resource_7"]]return = ["__START__ -> resource_5: 1.00","resource_5 -> __END__: 0.60","resource_5 -> resource_5: 0.20","resource_5 -> resource_7: 0.20","resource_7 -> __END__: 1.00"]Four users start at resource_5. Three of them stop there. user_12 goes on to resource_5 again and then to resource_7. So resource_5 is left 5 times: 3 to __END__, 1 to itself and 1 to resource_7.
Example 5
logs = [["1","a","home"],["2","a","cart"],["1","b","home"],["1","c","home"],["1","d","home"],["1","e","home"],["1","f","home"],["1","g","home"],["1","h","home"]]return = ["__START__ -> home: 1.00","cart -> __END__: 1.00","home -> __END__: 0.88","home -> cart: 0.13"]home is left 8 times: 7 times to __END__ and once to cart. 7/8 = 0.875 and 1/8 = 0.125 sit exactly halfway, so they round up to 0.88 and 0.13. Inside a group, __END__ is listed first.
Constraints
1 <= logs.length <= 10^4logs[i].length == 3timeis an integer from0to10^9, written without leading zeros.userhas1to20characters, each a lowercase letter, a digit or_.resourcehas1to20characters, each a lowercase letter, a digit or_, and starts with a letter.- No user has two entries with the same
time.
More WhatNot Phone Screen questions
- Merge Sorted Squares of Two ArraysPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Find Every Reachable Derived Vocabulary WordDuolingo · PHONE SCREENDuolingo interview questions
- Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions