Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,763 members, 7,820,657 topics. Date: Tuesday, 07 May 2024 at 06:56 PM

Solve This Amazon Interview Question - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Solve This Amazon Interview Question (2705 Views)

Can You Solve This Google Coding Interview Problem? (pics) / Chatgbt Is A Scam. It Cant Solve This Simple Thing I Ask Him / A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Solve This Amazon Interview Question by namikaze: 7:19pm On Oct 30, 2022
This problem was asked by Amazon.

Given a pivot x, and a list lst, partition the list into three parts.
* The first part contains all elements in lst that are less than x
* The second part contains all elements in lst that are equal to x
* The third part contains all elements in lst that are larger than x
Ordering within a part can be arbitrary.
For example, given x = 10 and lst = [9, 12, 3, 5, 14, 10, 10],
one partition may
be [9, 3, 5, 10, 10, 12, 14].

Can you solve it in strictly O(n) time and O(1) space complexity?
I'd like to see what you come up with.

4 Likes 2 Shares

Re: Solve This Amazon Interview Question by tensazangetsu20(m): 7:27pm On Oct 30, 2022
You can solve this using quick sort.

function QuickSort(Arr, x){
if(Arr.length <= 1){
return Arr;
}

const pivot = x
const leftArr = [];
const rightArr = [];

for(let i=0; i < Arr.length-1;i++){
Arr[i] < pivot ? leftArr.push(Arr[i]) : rightArr.push(Arr[i])
}

return [...QuickSort(leftArr, leftArr[leftArr.length-1]) ,pivot,...QuickSort(rightArr, rightArr[rightArr.length -1])];

}

console.log(QuickSort([9, 12, 3, 5, 14, 10, 10], 10))

1 Like

Re: Solve This Amazon Interview Question by Luckydonalds(m): 7:53pm On Oct 30, 2022
tensazangetsu20:
You can solve this using quick sort.
Quick sort does not satisfy OP's constraint, it has O(N) space complexity and O(nlogn) time complexity for its best case. In it's worst case, it can have an O(n**2) complexity

1 Like

Re: Solve This Amazon Interview Question by namikaze: 7:57pm On Oct 30, 2022
tensazangetsu20:
You can solve this using quick sort.

function QuickSort(Arr, x){
if(Arr.length <= 1){
return Arr;
}

const pivot = x
const leftArr = [];
const rightArr = [];

for(let i=0; i < Arr.length-1;i++){
Arr[i] < pivot ? leftArr.push(Arr[i]) : rightArr.push(Arr[i])
}

return [...QuickSort(leftArr, leftArr[leftArr.length-1]) ,pivot,...QuickSort(rightArr, rightArr[rightArr.length -1])];

}

console.log(QuickSort([9, 12, 3, 5, 14, 10, 10], 10))
Sure but the runtime to beat is O(n), that's the challenging part of the problem.
Re: Solve This Amazon Interview Question by tensazangetsu20(m): 7:57pm On Oct 30, 2022
Luckydonalds:

Quick sort does not satisfy OP's constraint, it has O(N) space complexity and O(nlogn) time complexity for its best case. In it's worst case, it can have an O(n**2) complexity

You can filter the array for elements based on ops suggestion and concat all the resultant arrays but that still takes extra space though.
Re: Solve This Amazon Interview Question by namikaze: 8:06pm On Oct 30, 2022
Since constant space is required, no additional data structure's allowed.
I was able to come up with a solution by modifying the quicksort's partitioning algorithm, so that's a hint.
Re: Solve This Amazon Interview Question by Luckydonalds(m): 8:08pm On Oct 30, 2022
namikaze:
Since constant space is required, no additional data structure's allowed.
I was able to come up with a solution by modifying the quicksort's partitioning algorithm, so that's a hint.
Do you mean that we should modify the array passed to the function directly?
Re: Solve This Amazon Interview Question by Nobody: 8:09pm On Oct 30, 2022
Can't believe Amazon ask such questions angry
Re: Solve This Amazon Interview Question by salvationproject(m): 8:10pm On Oct 30, 2022
May not be the most efficient way of doing this in JS but it is very much effective.


function partitionArray(array, x)
{
let lArray = [], eArray = [], gArray = [];
for (let i = 0; i < array.length; i++)
{
let v = array[i];
if (v < x)
{
lArray.push(v);
}
else if (v == x)
{
eArray.push(v);
}
else
{
gArray.push(v);
}
}
return lArray.concat(eArray).concat(gArray);
}

alert(partitionArray([9, 12, 3, 5, 14, 10, 10], 10));
Re: Solve This Amazon Interview Question by namikaze: 8:11pm On Oct 30, 2022
Luckydonalds:

Do you mean the we should modify the array passed to the function directly?
Yes, 2 pointers come into play in these context.
Re: Solve This Amazon Interview Question by namikaze: 8:12pm On Oct 30, 2022
GREATIGBOMAN:
Can't believe Amazon ask such questions angry
Too hard or too easy?
Re: Solve This Amazon Interview Question by bushjeph: 8:15pm On Oct 30, 2022
What sort of question be this?
Re: Solve This Amazon Interview Question by namikaze: 8:15pm On Oct 30, 2022
salvationproject:
May not be the most efficient way of doing this in JS but it is very much effective.


function partitionArray(array, x)
{
let lArray = [], eArray = [], gArray = [];
for (let i = 0; i < array.length; i++)
{
let v = array[i];
if (v < x)
{
lArray.push(v);
}
else if (v == x)
{
eArray.push(v);
}
else
{
gArray.push(v);
}
}
return lArray.concat(eArray).concat(gArray);
}

alert(partitionArray([9, 12, 3, 5, 14, 10, 10], 10));
Space complexity to beat is O(1), this constraint is what actually makes it a difficult FAANG level problem else just sorting the array would work too.
Re: Solve This Amazon Interview Question by Najdorf: 8:17pm On Oct 30, 2022
salvationproject:
May not be the most efficient way of doing this in JS but it is very much effective.


function partitionArray(array, x)
{
let lArray = [], eArray = [], gArray = [];
for (let i = 0; i < array.length; i++)
{
let v = array[i];
if (v < x)
{
lArray.push(v);
}
else if (v == x)
{
eArray.push(v);
}
else
{
gArray.push(v);
}
}
return lArray.concat(eArray).concat(gArray);
}

alert(partitionArray([9, 12, 3, 5, 14, 10, 10], 10));
.
Re: Solve This Amazon Interview Question by namikaze: 8:17pm On Oct 30, 2022
bushjeph:
What sort of question be this?
The type that Amazon asks grin grin,
too hard?
Re: Solve This Amazon Interview Question by bushjeph: 8:36pm On Oct 30, 2022
namikaze:
The type that Amazon asks grin grin, too hard?
what branch of subject i mean?
Re: Solve This Amazon Interview Question by namikaze: 8:40pm On Oct 30, 2022
bushjeph:
what branch of subject i mean?
Arrays, 2 pointers, partitioning, quicksort.

1 Like

Re: Solve This Amazon Interview Question by Hannania(m): 10:05pm On Oct 30, 2022
bushjeph:
what branch of subject i mean?
Lol...when you hear Indians and Chinese racking in six figures in dollars and migrating like birds, you thought Amazon was asking class components in React all these while abi grin...Thats the crazy shit of selecting 4000 employees from over 2 million applications.

6 Likes

Re: Solve This Amazon Interview Question by Enceladus(m): 10:18pm On Oct 30, 2022
two pointers solves this in O(n) at worst. This is probable the first question to wet your appetite. It's suspiciously easy. grin
Re: Solve This Amazon Interview Question by LikeAking: 10:48pm On Oct 30, 2022
Childs play....

Very useless question...


The question is very simple and straight forward Op sef no try, wetin be lst? Spell am well na @ list....

Let me break down this simple question...

You are given x assigned with a value of 10 and an array filled with lots of numbers '[9, 12, 3, 5, 14, 10, 10]'

Iterate through the array, find numbers less than x: = [3,5,9]

Iterate through the array, find numbers above x: = [12,14]

Iterate through the array, find numbers equal to x : = [10,10]

Explaining the question........

2 Likes

Re: Solve This Amazon Interview Question by Najdorf: 10:57pm On Oct 30, 2022
LikeAking:
Childs play....

Very useless question...


The question is very simple and straight forward Op sef no try, wetin be lst? Spell am well na @ list....

Let me break down this simple question...

You are given x assigned with a value of 10 and an array filled with lots of numbers '[9, 12, 3, 5, 14, 10, 10]'

Iterate through the array, find numbers less than x: = [

Iterate through the array, find numbers above than x.

Iterate through the array, find numbers equal than x.










This is why you're not working at Amazon sir grin

1 Like

Re: Solve This Amazon Interview Question by LikeAking: 11:02pm On Oct 30, 2022
Najdorf:

This is why you're not working at Amazon sir grin

This is a baby question...

I don pass this level.

Every thin na chance, if I get an Interview with Amazon , I will nail it...


@ Najdorf, e b like u just dey start?
Re: Solve This Amazon Interview Question by Najdorf: 11:18pm On Oct 30, 2022
LikeAking:


This is a baby question...

I don pass this level.

Every thin na chance, if I get an Interview with Amazon , I will nail it...


@ Najdorf, e b like u just dey start?




Truly, I just dey start boss. Might get serious next year
Re: Solve This Amazon Interview Question by Nobody: 11:26pm On Oct 30, 2022
LikeAking:


This is a baby question...

I don pass this level.

Every thin na chance, if I get an Interview with Amazon , I will nail it...


@ Najdorf, e b like u just dey start?






U just display your stupidity in HD


U don't even understand the actual thing they're looking for in the question.


I bet you don't even know what O(1) or O(n) means

grin

1 Like

Re: Solve This Amazon Interview Question by Nobody: 11:32pm On Oct 30, 2022
Najdorf:

This is why you're not working at Amazon sir grin


The way he post the nonsense boldly.
I wonder if he didn't see the solutions above which works but doesn't satisfy the questions Lol
Re: Solve This Amazon Interview Question by LikeAking: 11:39pm On Oct 30, 2022
GREATIGBOMAN:



U just display your stupidity in HD


U don't even understand the actual thing they're looking for in the question.


I bet you don't even know what O(1) or O(n) means

grin


U re just a big Fool. I am explaining something.
Re: Solve This Amazon Interview Question by LikeAking: 11:44pm On Oct 30, 2022
GREATIGBOMAN:



The way he post the nonsense boldly.
I wonder if he didn't see the solutions above which works but doesn't satisfy the questions Lol

Re: Solve This Amazon Interview Question by LikeAking: 2:13am On Oct 31, 2022
Najdorf:

This is why you're not working at Amazon sir grin


I don't need to work for Amazon Sir, I want to break the job hunting circle in my generation. I don't want my kids to look for jobs when they grow up, cos I know the job mkt will be over saturated by then.

My goal is to master the art of hustling and making money.. So my kids don't have to submit CVs..

I want to build my own Amazon.
Re: Solve This Amazon Interview Question by namikaze: 5:56am On Oct 31, 2022
Enceladus:
two pointers solves this in O(n) at worst. This is probable the first question to wet your appetite. It's suspiciously easy. grin
If it's that easy then explain your approach.
Re: Solve This Amazon Interview Question by TastyFriedPussy: 6:33am On Oct 31, 2022
tongue learn C++ them no go hear, now see as them just dey naked and disgrace theirselves, doing all sorts doing all sorts of rubbish with JS, something that would have been easier with C++ tongue
Re: Solve This Amazon Interview Question by TastyFriedPussy: 6:43am On Oct 31, 2022
tensazangetsu20:
You can solve this using quick sort.

function QuickSort(Arr, x){
if(Arr.length <= 1){
return Arr;
}

const pivot = x
const leftArr = [];
const rightArr = [];

for(let i=0; i < Arr.length-1;i++){
Arr[i] < pivot ? leftArr.push(Arr[i]) : rightArr.push(Arr[i])
}

return [...QuickSort(leftArr, leftArr[leftArr.length-1]) ,pivot,...QuickSort(rightArr, rightArr[rightArr.length -1])];

}

console.log(QuickSort([9, 12, 3, 5, 14, 10, 10], 10))
your own Na to rush into any coding challenge sharp sharp to go and solve rubbish, I've never seen any coding problem you solved on nairaland that is correct, never!!!! tongue

4 Likes

Re: Solve This Amazon Interview Question by Nobody: 8:57am On Oct 31, 2022
TastyFriedPussy:
your own Na to rush into any coding challenge sharp sharp to go and solve rubbish, I've never seen any coding problem you solved on nairaland that is correct, never!!!! tongue

grin grin grin grin
Genuinely cracked me up.

If that guy catch u ehn

(1) (2) (3) (Reply)

Does He Need Programming In Elect/elect.. / Can Vb 6.0,visual Studio 2008 And Visual Studio 2010 Run On Windows 7 Os. / There Is More To Software Development Than Programming

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 43
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.