FastPrepUser Resource Transition Probabilities

User Resource Transition Probabilities

Duolingo logoDuolingo● MediumINTERNPHONE SCREEN
Learn

Problem statement

You are given an unsorted event log logs. Each row has the form [time, user, resource], where time is a nonnegative decimal timestamp.

Build each user's chronological resource sequence independently. Sort that user's events by numeric timestamp; when two events have the same timestamp, preserve their original order in logs. Add START before the first resource and END after the last resource.

Count every adjacent transition across all user sequences. For each observed transition, return one row in the exact form current->next:count/total, where total is the number of outgoing transitions observed from current. Do not reduce the fraction.

Sort the returned rows lexicographically by current, then by next. User sequences never connect to one another.

Function

userResourceTransitionProbabilities(logs: String[][]) → String[]

Examples

Example 1

logs = [["3","alice","cart"],["1","alice","home"],["2","bob","search"],["2","alice","search"]]return = ["START->home:1/2","START->search:1/2","cart->END:1/1","home->search:1/1","search->END:1/2","search->cart:1/2"]

Alice's sequence is START, home, search, cart, END, while Bob's is START, search, END. Their transition counts are combined only after each sequence is complete.

Example 2

logs = [["5","user","b"],["5","user","a"],["4","other","c"]]return = ["START->b:1/2","START->c:1/2","a->END:1/1","b->a:1/1","c->END:1/1"]

The equal-time events for user keep their input order, so that sequence is START, b, a, END.

Example 3

logs = [["2","a","x"],["1","a","x"],["1","b","x"]]return = ["START->x:2/2","x->END:2/3","x->x:1/3"]

User a contributes x->x and one x->END; user b contributes the other x->END.

Constraints

  • 1 <= logs.length <= 2 * 10^5.
  • Every row contains exactly three strings: [time, user, resource].
  • 0 <= time <= 10^9, written as an ordinary decimal integer.
  • Each user contains 1 to 30 lowercase English letters or digits.
  • Each resource contains 1 to 30 lowercase English letters and is different from START and END.

More Duolingo problems

See Duolingo hiring insights
public String[] userResourceTransitionProbabilities(String[][] logs) {
    // Write your code here.
}
logs[["3","alice","cart"],["1","alice","home"],["2","bob","search"],["2","alice","search"]]
expected["START->home:1/2", "START->search:1/2", "cart->END:1/1", "home->search:1/1", "search->END:1/2", "search->cart:1/2"]
Checking account…