Created
March 21, 2022 16:24
-
-
Save KOMON/e6de690150a7867b9f8150003ae706d6 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Prompt: | |
* | |
* A customer has reported a bug in one of our APIs. The `/applicants` endpoint | |
* is designed to respond to search queries for applicants according to query | |
* parameters that are sent along with the request. | |
* | |
* For example, this request: | |
* GET /applicants?resume=1 | |
* | |
* Should return all applicants that have submitted a resume. | |
* | |
* This request: | |
* GET /applicants?country=USA&background_check=1 | |
* | |
* Should return all applicants from the USA that have passed a background check. | |
* | |
* | |
* However, the customer has reported that in response to this request: | |
* GET /applicants?resume=1&background_check=1&country=USA | |
* | |
* They seem to be getting back all USA applicants that have submitted a resume, | |
* whether or not they've actually passed a background check. | |
* | |
* After confirming that the problem is reproducible, we've narrowed down the | |
* problem to the controller that responds to `GET /applicants` requests below. | |
* | |
* We'll pair together on diagnosing the problem and seeing if we can fix the | |
* bug. Feel free to use any resources or reference materials you like, and feel | |
* free to ask me questions. | |
* | |
* More important than actually finding/fixing the issue is that I want to | |
* understand your thought process, so let me know what you're thinking and why | |
* you're taking the steps you're taking. | |
* | |
* You can simulate the request that causes the bug by pressing the 'Run' button | |
*/ | |
function getApplicants(params) { | |
// load all applicants from database | |
let applicants = APPLICANTS; | |
// We need to treat background checks and resumes special because they exist in different systems | |
if ('background_check' in params) { | |
applicants = filterByBackgroundChecks(applicants, params['background_check']); | |
delete params['background_check']; | |
} | |
if ('resume' in params) { | |
applicants = filterByResume(applicants, params['resume']); | |
delete params['resume']; | |
} | |
applicants = filterByParams(applicants, params); | |
return applicants; | |
} | |
function filterByBackgroundChecks(applicants, shouldHavePassed) { | |
// load background checks from external service | |
const backgroundChecks = BACKGROUND_CHECKS; | |
const checkPassedByName = {}; | |
for (const check of backgroundChecks) { | |
checkPassedByName[check.name] = check.passed; | |
} | |
const filtered = []; | |
for (const applicant of APPLICANTS) { | |
if (checkPassedByName[applicant.name] === shouldHavePassed) { | |
filtered.push(applicant); | |
} | |
} | |
return filtered; | |
} | |
function filterByResume(applicants, shouldHaveSubmitted) { | |
// load resumes from S3 | |
const resumes = RESUMES; | |
const resumeSubmittedByName = {}; | |
for (const resume of resumes) { | |
resumeSubmittedByName[resume.name] = resume.submitted; | |
} | |
const filtered = []; | |
for (const applicant of APPLICANTS) { | |
if (resumeSubmittedByName[applicant.name] === shouldHaveSubmitted) { | |
filtered.push(applicant); | |
} | |
} | |
return filtered; | |
} | |
function filterByParams(applicants, params) { | |
const filtered = []; | |
for (const applicant of applicants) { | |
let match = true; | |
for (const param in params) { | |
match = match && applicant[param] === params[param]; | |
if(!match) { | |
break; | |
} | |
} | |
if(match) { | |
filtered.push(applicant); | |
} | |
} | |
return filtered; | |
} | |
const APPLICANTS = [ | |
{ | |
name: 'Dan Raloff', | |
country: 'USA', | |
phone: '15555555555', | |
}, | |
{ | |
name: 'Spam Sawzall', | |
country: 'USA', | |
phone: '15555555555', | |
}, | |
{ | |
name: 'Sherm Wermbly', | |
country: 'USA', | |
phone: '15555555555', | |
}, | |
{ | |
name: 'Cat Dancin', | |
country: 'USA', | |
phone: '15555555555', | |
}, | |
{ | |
name: 'Danny Schmanny', | |
country: 'España', | |
phone: '15555555555', | |
}, | |
{ | |
name: 'Richy Rich', | |
country: 'Switzerland', | |
phone: '1$$$$$$$$$$' | |
}, | |
]; | |
const BACKGROUND_CHECKS = [ | |
{ name: 'Dan Raloff' , passed: 1 }, | |
{ name: 'Spam Sawzall' , passed: 0 }, | |
{ name: 'Sherm Wermbly' , passed: 1 }, | |
{ name: 'Cat Dancin' , passed: 0 }, | |
{ name: 'Danny Schmanny', passed: 0 }, | |
{ name: 'Richy Rich' , passed: 1 } | |
]; | |
const RESUMES = [ | |
{ name: 'Dan Raloff' , submitted: 1 }, | |
{ name: 'Spam Sawzall' , submitted: 1 }, | |
{ name: 'Sherm Wermbly' , submitted: 1 }, | |
{ name: 'Cat Dancin' , submitted: 0 }, | |
{ name: 'Danny Schmanny', submitted: 1 }, | |
{ name: 'Richy Rich' , submitted: 0 } | |
]; | |
console.log( | |
JSON.stringify( | |
getApplicants({ | |
country: 'USA', | |
resume: 1, | |
background_check: 1 | |
}), | |
null, | |
2 | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment