Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,763 members, 7,824,188 topics. Date: Saturday, 11 May 2024 at 03:48 AM

A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? (2689 Views)

Come And Solve This Coding Problem / Senior Software Programmers Needed Urgently For Remote Opportunities / A 30 Day Coding Challenge (Python) For BEGINNERS (2) (3) (4)

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

Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 6:13pm On May 29, 2022
MeMiTi:


Sorting will take nlogn time and priority queue will also take nlogn time since every insertion and deletion takes logn.

I think this might be the best solution, since you'll have to sort it one way or the other.
Yeah for the priority queue approach it's O(n log n), however it's not strictly O(n log n) for the other one.
In the worst case where all input array elements are unique then we'd be sorting an array of size n, so nlogn. The best case would be an input array of the same element then we'd be sorting an array of size 1, so O(n).
On average the input array would consist of both unique and repeated numbers, mostly repeated numbers, so we assume a time complexity of O(n) amortized.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Deicide: 9:00pm On May 29, 2022
namikaze:

Now it compiles, but the output is just a key - value pair of numbers and counts, you'd have to return an array/vector that satisfies the problem description.
That would not be difficult for you to do ryt? Cause the answer already shows in the key, I don't have strength for extra steps grin
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by omohayek: 9:24pm On May 29, 2022
MeMiTi:


Sorting will take nlogn time and priority queue will also take nlogn time since every insertion and deletion takes logn.

I think this might be the best solution, since you'll have to sort it one way or the other.
I disagree. There's no point sorting the array before counting the frequencies, as it's precisely those frequencies that you want to output in reverse-sorted order. Why do two sorts when you only need one? Scanning through all the items and putting them in the hashtable takes O(n) time, while the sort of the counts at the end will take O(n log n) time, but now the n is a much smaller value.

In any case, this sort of trivial question is not the sort of thing one should expect when applying for a "senior developer" position. At best it's useful as a means of weeding out the totally hopeless before wasting any time giving them personal interviews. This is just a slightly fancier "FizzBuzz" question.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by salvationproject(m): 9:30pm On May 29, 2022
Looks simple but demands some kinda deep thinking... Here is the solution in JavaScript,

function sort(arr){
let pre_sorted_arr = {}, sorted_arr = [];
for (i = 0; i < arr.length; i++){
if (pre_sorted_arr[arr[i]] === undefined)
{
pre_sorted_arr[arr[i]] = 1;
}
else
{
pre_sorted_arr[arr[i]] += 1;
}
}

while (Object.keys(pre_sorted_arr).length > 0)
{
for (x in pre_sorted_arr)
{
let j = 1;
for (i in pre_sorted_arr)
{
if (pre_sorted_arr[x] != pre_sorted_arr[i] && pre_sorted_arr[x] < pre_sorted_arr[i])
{
j = 0;
}
}
if (j == 1)
{
sorted_arr.push(x);
delete pre_sorted_arr[x];
}
}
}

return sorted_arr;
}

alert(sort([1,2,6,2,3,7,7,5,7,6,2,7])); //OUTPUTS: 7,2,6,1,3,5
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 11:03pm On May 29, 2022
Deicide:
That would not be difficult for you to do ryt? Cause the answer already shows in the key, I don't have strength for extra steps grin
Yeah it's in the keys but it's not as straightforward as you make it seem. It is not me you will torture with C++ grin, the lord shall be your strength lol.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 11:15pm On May 29, 2022
omohayek:

I disagree. There's no point sorting the array before counting the frequencies, as it's precisely those frequencies that you want to output in reverse-sorted order. Why do two sorts when you only need one? Scanning through all the items and putting them in the hashtable takes O(n) time, while the sort of the counts at the end will take O(n log n) time, but now the n is a much smaller value.

In any case, this sort of trivial question is not the sort of thing one should expect when applying for a "senior developer" position. At best it's useful as a means of weeding out the totally hopeless before wasting any time giving them personal interviews. This is just a slightly fancier "FizzBuzz" question.
Nope it's not the frequencies, it's the input array sorted by the frequencies of it's elements, you missed the whole point of the question, so he's not wrong.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 11:22pm On May 29, 2022
salvationproject:
Looks simple but demands some kinda deep thinking... Here is the solution in JavaScript,

function sort(arr){
let pre_sorted_arr = {}, sorted_arr = [];
for (i = 0; i < arr.length; i++){
if (pre_sorted_arr[arr[i]] === undefined)
{
pre_sorted_arr[arr[i]] = 1;
}
else
{
pre_sorted_arr[arr[i]] += 1;
}
}

while (Object.keys(pre_sorted_arr).length > 0)
{
for (x in pre_sorted_arr)
{
let j = 1;
for (i in pre_sorted_arr)
{
if (pre_sorted_arr[x] != pre_sorted_arr[i] && pre_sorted_arr[x] < pre_sorted_arr[i])
{
j = 0;
}
}
if (j == 1)
{
sorted_arr.push(x);
delete pre_sorted_arr[x];
}
}
}

return sorted_arr;
}

alert(sort([1,2,6,2,3,7,7,5,7,6,2,7])); //OUTPUTS: 7,2,6,1,3,5
Works well, nice one, do you mind explaining your approach?
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by omohayek: 8:44am On May 30, 2022
namikaze:

Nope it's not the frequencies, it's the input array sorted by the frequencies of it's elements, you missed the whole point of the question, so he's not wrong.
You couldn't have made a dumber reply if you tried. How exactly are you going to output the "array sorted by the frequencies of it's [sic] elements" without sorting the frequencies, which is exactly what I said? Given your lack of English comprehension skills, it's no wonder a trivial question like this would strike you as the sort of thing a "senior software developer" would be asked as anything more than a screener.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 9:21am On May 30, 2022
omohayek:

You couldn't have made a dumber reply if you tried. How exactly are you going to output the "array sorted by the frequencies of it's [sic] elements" without sorting the frequencies, which is exactly what I said? Given your lack of English comprehension skills, it's no wonder a trivial question like this would strike you as the sort of thing a "senior software developer" would be asked as anything more than a screener.
Ah shit here we go again, typical Nigerian, you said "it's precisely those frequencies that you want to output in reverse-sorted order", which doesn't apply in the context of this problem. Like I said, you're a typical Nigerian, take your frustrations elsewhere.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by omohayek: 10:55am On May 30, 2022
namikaze:

Ah shit here we go again, typical Nigerian, you said "it's precisely those frequencies that you want to output in reverse-sorted order", which doesn't apply in the context of this problem. Like I said, you're a typical Nigerian, take your frustrations elsewhere.
God, you're dumber than a post! This "typical Nigerian" has turned down offers by the very same Facebook from which you got your silly little question from, and has been making a European lead engineer's salary since before you even started secondary school, but in your stupidity and arrogance you think to talk down to me? What a laugh!

I guess it's my fault for bothering to contribute to such a cretinous thread in the first place. Stay out of my mentions and enjoy masturbating to whatever delusions of "seniority" your low IQ leads you to indulge in. My time is valuable and not worth wasting on mouth-breathers like you.

2 Likes

Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 11:31am On May 30, 2022
omohayek:

God, you're dumber than a post! This "typical Nigerian" has turned down offers by the very......garbage
Bye bye.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by gistray: 9:46pm On Jun 04, 2022
i forgot his question, just strolled back and decide to continue short solution lol.... i didnt care for time complexity or anything.

function SortArrayFrequency(arr){

// set number to one
var num = 1
let finalArray=[]
let arrayByFrequency = []

// auto sorting given array & Iterate through;
for(let i=0; i<arr.sort().length; i++){

// if next sorted array item matches previous add 1 to num;
if(arr[i]==arr[i+1]){
num++
}
else{
//else push the item to sorted array by frequecy and set num back to 1
arrayByFrequency.push([arr[i],num])
num=1
}
}

// Now sort arrayByfrequecy, reverse it and then add to final array and return
arrayByFrequency.sort(function(a, b) {
return a[1] - b[1];
}).reverse().forEach((item)=>{
finalArray.push(item[0])
})
return finalArray

}

SortArrayFrequency([1,2,6,2,3,7,7,5,7,6,2,7])
(6) [7, 2, 6, 5, 3, 1]
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 10:09pm On Jun 04, 2022
gistray:
i forgot his question, just strolled back and decide to continue short solution lol.... i didnt care for time complexity or anything.

function SortArrayFrequency(arr){

// set number to one
var num = 1
let finalArray=[]
let arrayByFrequency = []

// auto sorting given array & Iterate through;
arr.sort();
for(let i=0; i<arr.length; i++){

// if next sorted array item matches previous add 1 to num;
if(arr[i]==arr[i+1]){
num++
}
else{
//else push the item to sorted array by frequecy and set num back to 1
arrayByFrequency.push([arr[i],num])
num=1
}
}

// Now sort arrayByfrequecy, reverse it and then add to final array and return
arrayByFrequency.sort(function(a, b) {
return a[1] - b[1];
}).reverse().forEach((item)=>{
finalArray.push(item[0])
})
return finalArray

}

SortArrayFrequency([1,2,6,2,3,7,7,5,7,6,2,7])
(6) [7, 2, 6, 5, 3, 1]
Works well, time complexity of O(n log n), nice one.
You should've sort the array outside the for loop though.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Nobody: 1:25am On Jun 05, 2022
namikaze:

Works well, time complexity of O(n log n), nice one.
You should've sort the array outside the for loop though.




I didn't I didn't want to create a duplicate array hence why I used short hand
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Chuukwudi(m): 2:28am On Jun 05, 2022
namikaze:
Apparently a senior software developer could not solve this coding interview problem, can you?

Given an array of numbers, list them in the order of most repeated. I.e:
[1,2,6,2,3,7,7,5,7,6,2,7] => [7,2,6,5,3,1]

source: ``https://www./programmer.nullposting/permalink/1456356328162617/?app=fbl``

import collections
def SortMe(lst):
counts = collections.Counter(lst)
return set(sorted(lst, key=lambda x: -counts[x]))
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 7:52am On Jun 05, 2022
Chuukwudi:


import collections
def SortMe(lst):
counts = collections.Counter(lst)
return set(sorted(lst, key=lambda x: -counts[x]))

Your solution is incorrect, does not pass the test cases, take a look at the description, you must've missed something.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 7:54am On Jun 05, 2022
GREATIGBOMAN:





I didn't I didn't want to create a duplicate array hence why I used short hand

I think sorting in JavaScript in an in place operation, e.g;
// arr.sort();
I may be wrong though.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Nobody: 9:58am On Jun 05, 2022
namikaze:

I think sorting in JavaScript in an in place operation, e.g;
// arr.sort();
I may be wrong though.
Oh shoot


Just saw that now lol.

That was have happened when I tried pasting on nairaland.

I've modified

1 Like

Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Nobody: 9:59am On Jun 05, 2022
gistray:
i forgot his question, just strolled back and decide to continue short solution lol.... i didnt care for time complexity or anything.

function SortArrayFrequency(arr){

// set number to one
var num = 1
let finalArray=[]
let arrayByFrequency = []

// auto sorting given array & Iterate through;



for(let i=0; i<arr.sort().length; i++){

// if next sorted array item matches previous add 1 to num;
if(arr[i]==arr[i+1]){
num++
}
else{
//else push the item to sorted array by frequecy and set num back to 1
arrayByFrequency.push([arr[i],num])
num=1
}
}

// Now sort arrayByfrequecy, reverse it and then add to final array and return
arrayByFrequency.sort(function(a, b) {
return a[1] - b[1];
}).reverse().forEach((item)=>{
finalArray.push(item[0])
})
return finalArray

}

SortArrayFrequency([1,2,6,2,3,7,7,5,7,6,2,7])
(6) [7, 2, 6, 5, 3, 1]
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 1:03pm On Jun 05, 2022
GREATIGBOMAN:
Oh shoot

Just saw that now lol.
That was have happened when I tried pasting on nairaland.
I've modified
Noted.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by excanny: 7:23pm On Jun 05, 2022
Did it in C#. I put the data into a hashmap, ordered the values and put the keys back into as a new sorted array. But space complexicity was crazy. Had to initiate about 3 memory allocations. What's the expected time and space complexity for the problem?

1 Like

Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 10:15pm On Jun 05, 2022
excanny:
Did it in C#. I put the data into a hashmap, ordered the values and put the keys back into as a new sorted array. But space complexicity was crazy. Had to initiate about 3 memory allocations. What's the expected time and space complexity for the problem?
The expected time and space complexity are both O(n). My approach is almost same as yours, I did only 2 memory allocations though. Nice one smiley.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Deicide: 10:24am On Oct 11, 2022
namikaze:

The expected time and space complexity are both O(n). My approach is almost same as yours, I did only 2 memory allocations though. Nice one smiley.
if you sorting then the time complexity is not O(n)
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 11:51am On Oct 12, 2022
Deicide:
if you sorting then the time complexity is not O(n)
yeah, I missed that, in the worst case it's O n log n i.e there are no duplicates but in the average the time should be lower.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by BeLookingIDIOT(m): 7:26pm On Oct 12, 2022
There should be a method for finding the most frequent element in an array/arraylist,and also one for removing all occurrences of an element from an array/arraylist.
Find the most frequent element in the given array and add it the new array, afterwards you remove all occurrences of it from the given array. Repeat until the given array is empty.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Nobody: 2:28pm On Oct 13, 2022
namikaze:

This addresses the first part of the problem only, remember, even elements with a count of 1 should be in the output array, also hashmaps are not ordered by default so you'd have to address that too.

You can use TreeMaps which are ordered, you can also sort the maps after your logic. This problem is an easy one to be fair, if i see this type of problem in an interview. I will be praising the name of the Lord.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 8:30am On Oct 14, 2022
truthsayer009:


You can use TreeMaps which are ordered, you can also sort the maps after your logic. This problem is an easy one to be fair, if i see this type of problem in an interview. I will be praising the name of the Lord.
The runtime would still be O n log n but yeah you're correct and yes it's a simple question, that's why I had to post it.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by obimerchant: 9:14am On Oct 14, 2022
I am really wowed by all this I am seeing here...
really thought I was a good developer but nah....
can't even understand all this things you all are posting here
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Guontey(m): 1:57pm On Oct 14, 2022
namikaze:
My solution:
def sort_by_frequency(arr):
fmap = {}
for num in arr:
fmap[num] = fmap.get(num, 0) + 1
arr = [ (v,k) for k,v in fmap.items() ]
arr.sort(reverse=True)
return [ b for a,b in arr ]

Let's see yours.

You can write it in one line of code if you can import Counter

a = [1,2,6,2,3,7,7,5,7,6,2,7]
from collections import Counter
list(list(zip(*Counter(a).most_common()))[0])
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 6:05pm On Oct 14, 2022
Guontey:


You can write it in one line of code if you can import Counter

a = [1,2,6,2,3,7,7,5,7,6,2,7]
from collections import Counter
list(list(zip(*Counter(a).most_common()))[0])
+1 for the Counter component, but the resulting code's really hard to read especially to non python devs, reason I wrote it plainly.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by namikaze: 6:08pm On Oct 14, 2022
obimerchant:
I am really wowed by all this I am seeing here...
really thought I was a good developer but nah....
can't even understand all this things you all are posting here
It's all core DSA, you might be a good developer but who knows, depends on your domain.
Re: A "Senior" Software Developer Could Not Solve This Coding Challenge, Can You? by Nobody: 9:41pm On Oct 14, 2022
obimerchant:
I am really wowed by all this I am seeing here...
really thought I was a good developer but nah....
can't even understand all this things you all are posting here

How can u be good developer if u don't understand basic algo.


Just accept your WordPress theme installer

(1) (2) (Reply)

Chat Room Application / Please How Did You Solve Your Power Need Without Use Of Generator / Open Sourcing Sachng, I Need Your Advice And Idea

(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. 58
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.