Last active
October 9, 2022 14:09
-
-
Save dmitry-tuzenkov/f0a51b40e54b6330be990324205806c3 to your computer and use it in GitHub Desktop.
Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here is our log file; essentially a list of tuples. | |
// Each tuple has two items: `(request_path, user_id)` | |
const logFile = [ | |
["/", "user_1"], | |
["/about", "user_1"], | |
["/", "user_3"], | |
["/features", "user_1"], | |
["/about", "user_2"], | |
["/purchase", "user_2"], | |
["/purchase", "user_1"], | |
["/thank-you", "user_1"], | |
["/about", "user_3"], | |
["/thank-you", "user_2"], | |
["/purchase", "user_3"], | |
["/thank-you", "user_3"] | |
]; | |
// A triplet is a group of three consecutive pages visited by the same user. For example, | |
// user_1 visited the following pages in this order: | |
// '/' , '/about', '/features', '/purchase', '/thank-you' | |
// | |
// In this case, user_1 created three unique triplets during her visit: | |
// 1. ['/', '/about', '/features'] | |
// 2. ['/about', '/features', '/purchase'] | |
// 3. ['/features', '/purchase', '/thank-you'] | |
// | |
// At the same time, user_3 went through a different workflow but created two unique triplets during his visit: | |
// 1. ['/', '/about', '/purchase'] | |
// 2. ['/about', '/purchase', '/thank-you'] | |
// | |
// Your function should return the 10 most occurring triplets in the log file. Given the log file above, the | |
// returned list should something look like: | |
// | |
// [ | |
// ['/about', '/purchase', '/thank-you'], // cnt 2 | |
// ['/', '/about', '/features'], // cnt 1 | |
// ['/about', '/features', '/purchase'], // cnt 1 | |
// ['/features', '/purchase', '/thank-you'], // cnt 1 | |
// ['/', '/about', '/purchase'], //cnt 1 | |
// ] | |
// tuple3([0,1,2,3,4]) => [[0,1,2], [1,2,3], [2,3,4]] | |
const tuple3 = (arr = [], n = 3) => { | |
return arr.reduce((acc, x, i) => { | |
if (arr.length - n < i) { | |
return acc; | |
} | |
const _arr = [...arr]; | |
const p = _arr.slice(i, i + n); | |
acc.push(p); | |
return acc; | |
}, []); | |
}; | |
const fn = (logArr = []) => { | |
const group = logArr.reduce((acc, x) => { | |
const [p, u] = x; | |
if (!acc[u]) { | |
acc[u] = []; | |
} | |
acc[u].push(p); | |
return acc; | |
}, {}); | |
console.trace("group", group); | |
const tuples = Object.values(group) | |
.map((x) => tuple3(x)) | |
.flat(); | |
console.trace("tuples", tuples); | |
const top10 = tuples.reduce((acc, x) => { | |
const key = JSON.stringify(x); | |
if (!acc[key]) { | |
acc[key] = 0; | |
} | |
acc[key] += 1; | |
return acc; | |
}, {}); | |
console.trace("top10", top10); | |
return Object.entries(top10).sort((a, b) => b[1] - a[1]); | |
}; | |
console.log("Final Result", fn(logFile)); | |
/** | |
[ | |
[ | |
"[\"/about\",\"/purchase\",\"/thank-you\"]", | |
2 | |
], | |
[ | |
"[\"/\",\"/about\",\"/features\"]", | |
1 | |
], | |
[ | |
"[\"/about\",\"/features\",\"/purchase\"]", | |
1 | |
], | |
[ | |
"[\"/features\",\"/purchase\",\"/thank-you\"]", | |
1 | |
], | |
[ | |
"[\"/\",\"/about\",\"/purchase\"]", | |
1 | |
] | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment