Amazon Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
1
of 3 vote

Create two maps A) User -> BW - each time update BW for that user,
and B) Country -> MaxHeap( Object holding User and BW ) based on BW
(i) Get total BW by map.get(user)
(ii) Get top 5 by popping max element and rebuilding the heap - O(klogk) k = 5.

- Sunny August 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

complexity would be O(klogn) where n is number of user in a country.

- rishabdev919 August 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = ["User:A Bandwidth:50 CountryCode:IND",
"User:B Bandwidth:60 CountryCode:USA",
"User:C Bandwidth:60 CountryCode:IND",
"User:D Bandwidth:80 CountryCode:IND",
"User:A Bandwidth:70 CountryCode:IND"]


const parsed = a.map(line => {
const [user, bandwidth, countryCode] = line.split(' ').map(value=>value.split(':')[1])
return { user, bandwidth, countryCode}
})

const users = parsed.reduce( (output, {user,bandwidth,countryCode}) => {
const existing = output[user]
output[user] = {
bandwidth: parseInt(bandwidth) + (existing? parseInt(existing.bandwidth) : 0),
countryCode
}
return output
} , {})



const countries = Object.entries(users).reduce( (output, [user, {bandwidth, countryCode}]) => {
const existing = output[countryCode]
const rank = existing? [...existing.rank, {user,bandwidth}] : [{user,bandwidth}]
output[countryCode] = {
rank: rank.sort((a,b)=> b.bandwidth - a.bandwidth).slice(0,5)
}
return output;
}
,{})

console.log(JSON.stringify(countries))

- Anonymous August 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = ["User:A Bandwidth:50 CountryCode:IND",
"User:B Bandwidth:60 CountryCode:USA",
"User:C Bandwidth:60 CountryCode:IND",
"User:D Bandwidth:80 CountryCode:IND",
"User:A Bandwidth:70 CountryCode:IND"]


const parsed = a.map(line => {
  const [user, bandwidth, countryCode] = line.split(' ').map(value=>value.split(':')[1])
  return { user, bandwidth, countryCode}
})

const users = parsed.reduce( (output, {user,bandwidth,countryCode}) => {
  const existing = output[user]
  output[user] = {
    bandwidth: parseInt(bandwidth) + (existing? parseInt(existing.bandwidth) : 0),
    countryCode
  }
  return output
} , {})



const countries = Object.entries(users).reduce( (output, [user, {bandwidth, countryCode}]) => {
  const existing = output[countryCode]
  const rank = existing? [...existing.rank, {user,bandwidth}] : [{user,bandwidth}]
  output[countryCode] = {
    rank: rank.sort((a,b)=> b.bandwidth - a.bandwidth).slice(0,5)
  }
  return output;
}
,{})

console.log(JSON.stringify(countries))

- Anonymous August 20, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More