Created
February 19, 2020 03:11
-
-
Save misterpoloy/bd1f843026ef8f0b88554dbd3a1b96d3 to your computer and use it in GitHub Desktop.
Twitter Code Interview
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
const CHUNKS = 3; | |
/** @paran {array} input */ | |
function mostFrecuentHistory(input) { | |
const globalCounter = {} | |
input.forEach(arr => { | |
arr.forEach(log => { | |
if (!globalCounter[log]) { | |
globalCounter[log] = 0; | |
} | |
globalCounter[log]++; | |
}); | |
}); | |
let maxValue = -1; | |
let maxKey = -1; | |
Object.keys(globalCounter).forEach(key => { | |
if (maxValue < globalCounter[key]) { | |
maxKey = key; | |
maxValue = globalCounter[key]; | |
} | |
}); | |
console.log("LONG: ", maxKey, "Times: ", maxValue); | |
} | |
/** @paran {string} input, @returns {object} */ | |
function splitIntoUsers(input) { | |
const tweets = input.split(' '); | |
const tweetsByUser = {} | |
for (let i = 0; i < tweets.length; i++) { | |
const tweet = tweets[i].split(','); | |
const username = tweet[0]; | |
const tweet_id = tweet[1]; | |
if (!tweetsByUser[username]) { | |
tweetsByUser[username] = []; | |
} | |
tweetsByUser[username].push(tweet_id); | |
} | |
return tweetsByUser; | |
} | |
/** @paran {array} input, @returns {array} */ | |
function findHistoryForInput(tweets) { | |
let globaCounter = []; | |
let temporalChunk = []; | |
let firstTime = false; | |
for (let i = 0; i < tweets.length; i++) { | |
const tweet_id = tweets[i]; | |
if (CHUNKS > i) { | |
// Is one of the first 3 elments, create chunk (*) | |
temporalChunk.push(tweet_id); | |
} else { | |
// (*) First iteration | |
if (temporalChunk.length == CHUNKS && firstTime == false) { | |
globaCounter.push(temporalChunk.join(" ")); | |
firstTime = true; | |
} | |
// Start look for the last 2 (CHUNKS - 1) | |
temporalChunk = [ temporalChunk[CHUNKS - 2], temporalChunk[CHUNKS - 1], tweet_id]; | |
globaCounter.push(temporalChunk.join(" ")); | |
} | |
} | |
return globaCounter; | |
} | |
const input = "@Biz,T1 @Jack,T7 @Biz,T2 @Jack,T8 @Biz,T3 @Biz,T3 @Biz,T2 @Biz,T1 @Biz,T2 @Jack,T9 @Jack,T7 @Jack,T8 @Jack,T9 @Biz,T3 @Biz,T7 @Biz,T8 @Biz,T9"; | |
function main() { | |
// Split into user. | |
const userLogs = splitIntoUsers(input); | |
// Get the history by user | |
const historyByUser = Object.keys(userLogs).map(key => { | |
const dataByUser = userLogs[key]; | |
return findHistoryForInput(dataByUser); | |
}); | |
// Get the most frequent history | |
mostFrecuentHistory(historyByUser); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken notes
