Skip to content

Instantly share code, notes, and snippets.

@salimdellali
Last active January 30, 2023 23:58
Show Gist options
  • Save salimdellali/294dac6a97eb81a8e84c958dfbd9ce3b to your computer and use it in GitHub Desktop.
Save salimdellali/294dac6a97eb81a8e84c958dfbd9ce3b to your computer and use it in GitHub Desktop.
/**
Prompt: Given a list of session identifiers and requested paths in the form
`[{session_id: str, path: str}, …]`, return, in descending order,
the sessions with the most path changes, along with the number of changes for each.
Example:
Input
```
[
{"session_id": "aec5075b8f0c40538f40b3ad65ac6196", "path": "/payments"},
{"session_id": "aec5075b8f0c40538f40b3ad65ac6196", "path": "/upgrade"},
{"session_id": "72c04277ec21427e89a71b50d58f2e0d", "path": "/details"},
{"session_id": "8ec2b63530634f839b5a106888145fb8", "path": "/payments"},
{"session_id": "cbd86924fdd84af885a63350a78f1c09", "path": "/details"},
{"session_id": "9930b99fd96843f58edd65e49f56c8b9", "path": "/payments"},
{"session_id": "72c04277ec21427e89a71b50d58f2e0d", "path": "/home"},
{"session_id": "8ec2b63530634f839b5a106888145fb8", "path": "/details"},
{"session_id": "425d08e4b32a491cbedff82a622e3a27", "path": "/support"},
{"session_id": "aec5075b8f0c40538f40b3ad65ac6196", "path": "/upgrade"},
{"session_id": "9930b99fd96843f58edd65e49f56c8b9", "path": "/payments"},
{"session_id": "aec5075b8f0c40538f40b3ad65ac6196", "path": "/details"},
{"session_id": "72c04277ec21427e89a71b50d58f2e0d", "path": "/support"},
{"session_id": "8ec2b63530634f839b5a106888145fb8", "path": "/payments"},
{"session_id": "425d08e4b32a491cbedff82a622e3a27", "path": "/support"},
{"session_id": "aec5075b8f0c40538f40b3ad65ac6196", "path": "/home"},
{"session_id": "9930b99fd96843f58edd65e49f56c8b9", "path": "/support"},
{"session_id": "cbd86924fdd84af885a63350a78f1c09", "path": "/upgrade"}
]
```
Output
```
[
{"session_id": "aec5075b8f0c40538f40b3ad65ac6196", "count": 4},
{"session_id": "72c04277ec21427e89a71b50d58f2e0d", "count": 3},
{"session_id": "8ec2b63530634f839b5a106888145fb8", "count": 3},
{"session_id": "cbd86924fdd84af885a63350a78f1c09", "count": 2},
{"session_id": "9930b99fd96843f58edd65e49f56c8b9", "count": 2},
{"session_id": "425d08e4b32a491cbedff82a622e3a27", "count": 1}
]
```
*/
const INPUT_ARR = [
{ session_id: 'aec5075b8f0c40538f40b3ad65ac6196', path: '/payments' },
{ session_id: 'aec5075b8f0c40538f40b3ad65ac6196', path: '/upgrade' },
{ session_id: '72c04277ec21427e89a71b50d58f2e0d', path: '/details' },
{ session_id: '8ec2b63530634f839b5a106888145fb8', path: '/payments' },
{ session_id: 'cbd86924fdd84af885a63350a78f1c09', path: '/details' },
{ session_id: '9930b99fd96843f58edd65e49f56c8b9', path: '/payments' },
{ session_id: '72c04277ec21427e89a71b50d58f2e0d', path: '/home' },
{ session_id: '8ec2b63530634f839b5a106888145fb8', path: '/details' },
{ session_id: '425d08e4b32a491cbedff82a622e3a27', path: '/support' },
{ session_id: 'aec5075b8f0c40538f40b3ad65ac6196', path: '/upgrade' },
{ session_id: '9930b99fd96843f58edd65e49f56c8b9', path: '/payments' },
{ session_id: 'aec5075b8f0c40538f40b3ad65ac6196', path: '/details' },
{ session_id: '72c04277ec21427e89a71b50d58f2e0d', path: '/support' },
{ session_id: '8ec2b63530634f839b5a106888145fb8', path: '/payments' },
{ session_id: '425d08e4b32a491cbedff82a622e3a27', path: '/support' },
{ session_id: 'aec5075b8f0c40538f40b3ad65ac6196', path: '/home' },
{ session_id: '9930b99fd96843f58edd65e49f56c8b9', path: '/support' },
{ session_id: 'cbd86924fdd84af885a63350a78f1c09', path: '/upgrade' },
];
type PathUsage = {
lastPath: string;
count: number;
};
type Sessions = {
[key: string]: PathUsage;
};
const sessionsObj: Sessions = {};
// build the sessionsObj
INPUT_ARR.forEach((input) => {
// I could have used the object deconstructuring const { session_id, path } = input;
// but for a better readability it's prefererable
// to use input.session_id and input.path
const sessionObjKeysArr = Object.keys(sessionsObj);
if (sessionObjKeysArr.includes(input.session_id)) {
// the input.session_id is already inside sessionsObj
// check the path
if (sessionsObj[input.session_id].lastPath !== input.path) {
// increment count and update lastPath
sessionsObj[input.session_id] = {
lastPath: input.path,
count: sessionsObj[input.session_id].count + 1,
};
}
} else {
// the input.session_id isn't inside the sessionsObj
// create new entry and initialize count & lastPath
sessionsObj[input.session_id] = {
lastPath: input.path,
count: 1,
};
}
});
// construct the output array
const outputArr = Object.entries(sessionsObj).map((sessionsObjEntry) => {
const [session_id, { count }] = sessionsObjEntry;
return {
session_id,
count,
};
});
// sort the output array in descending order
outputArr.sort((a, b) => {
return b.count - a.count;
});
console.log(outputArr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment