Skip to content

Instantly share code, notes, and snippets.

@NeuTrix
Created January 13, 2022 20:50
Show Gist options
  • Save NeuTrix/74a1036b48fedf8994d6b58713341acb to your computer and use it in GitHub Desktop.
Save NeuTrix/74a1036b48fedf8994d6b58713341acb to your computer and use it in GitHub Desktop.
Solution to lattice interview problem (from memory)
// -- The "API" fakes
const surveys = [
{ id: "surveyID-01",name: "Survey A" },
{ id: "surveyID-02",name: "Survey B" },
]
const prompts = [
{
id: 'promptId-01',
survey_id: 'surveyID-01',
prompt: 'How much did you enjoy working with Walker?',
}
,
{
id: 'promptId-02',
survey_id: 'surveyID-01',
prompt: 'How much are you impressed (or concerned) that he followed up with this?',
},
{
id: 'promptId-03',
survey_id: 'surveyID-03',
prompt: 'Needed a filler for the test?',
}
]
const responses = [
{ id: 'responseId-01',question_id: 'promptId-01',score: 10,},
{ id: 'responseId-02',question_id: 'promptId-01',score: -1,},
{ id: 'responseId-03',question_id: 'promptId-01',score: 5,},
{ id: 'responseId-04',question_id: 'promptId-01',score: 5,},
{ id: 'responseId-05',question_id: 'promptId-02',score: -2,},
{ id: 'responseId-06',question_id: 'promptId-02',score: 9,},
{ id: 'responseId-07',question_id: 'promptId-02',score: 2,},
{ id: 'responseId-08',question_id: 'promptId-03',score: 1,},
{ id: 'responseId-09',question_id: 'promptId-01',score: 9,},
{ id: 'responseId-10',question_id: 'promptId-02',score: 7,}
]
const fakeApiCall = {
surveys,prompts,responses
}
// ----------------
function cantLetItGo(surveyID) {
// pretend we make a fetch/get call to the api
const surveys = fakeApiCall.surveys;
const prompts = fakeApiCall.prompts;
const responses = fakeApiCall.responses;
// console.dir({ surveys,prompts })
// get and display the name
const targetSurvey = surveys.filter(s => s.id === surveyID)[0];
// get the survey prompts
const targetPrompts = prompts.filter(p => p.survey_id === surveyID);
// console.log({ targetPrompts });
// collect the prompt scores in an dict/array
const promptMap = new Map();
targetPrompts.forEach(tp => {
const scores = responses.filter(r => r.question_id === tp.id).map(sc => sc.score);
let sum = scores.reduce((sum,s) => sum + s);
// could do use Math.ceil but assume this current format is ok
let average = sum / scores.length;
let dist = new Map();
dist.set('Yup',0);
dist.set('Meh',0);
dist.set('Naw',0);
// this is clunky and can use a refactor
scores.forEach(s => {
if (s >= 6) {
let curr = dist.get('Yup');
dist.set('Yup',curr += 1);
}
if (s <= 5 && s >= 3) {
let curr = dist.get('Meh');
dist.set('Meh',curr += 1);
}
if (s < 3) {
let curr = dist.get('Naw');
dist.set('Naw',curr += 1);
}
})
promptMap.set(tp.id,{
prompt: tp.prompt,
scores,
average,
dist,
})
})
const printData = (map) => {
console.log(map); // FYI
console.log(`\n\n${targetSurvey.name} \n-------------- \n`);
map.forEach(val => {
console.log(`****\n"${val.prompt}" `);
console.log(`Average: ${val.average}`);
let dist = val.dist; // dist sub map
let distStr = '';
dist.forEach((val,key) => {
distStr += `${key}: ${val} \t`;
})
console.log(`Distribution: ${distStr} \n`);
})
}
printData(promptMap)
}
// run the program...
const SURVEY_ID = "surveyID-01";
cantLetItGo(SURVEY_ID)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment