d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Algorithm Question
12Next
Add Reply New Topic New Poll
Member
Posts: 5,000
Joined: Jul 4 2009
Gold: 118.00
Aug 3 2018 02:07pm
I was applying to a company as a software developer (I am student, so it would have been an internship). On the interview there was a question I was not able to answer and still do not know.
The question:
We have an array of 100 integer, which is sorted. Someone choses 5 randomly and changes their value. Which is the fastest/best method to make them sorted again?
I asked some friends who has programming knowledge (one of them is a game programmer) and none of them was able to give me a solution.
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Aug 3 2018 07:41pm
What language? Experienced developers don't write sorting algorithms, but you should learn and implemented a few in school. If this was java I'd just do:
Code
Arrays.sort(arr);


The sorting algorithm doesn't really matter when only sorting 100 integers. If you know about the data you're working with and you're going to run this sort a lot or for a large data set then you can implement a specific algorithm. Here is a reference I like: http://bigocheatsheet.com/

If you want to learn some sorting algorithms then I'd probably learn quicksort and bubble sort. They shouldn't take you longer than 10-15 minutes to understand.
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Aug 3 2018 07:42pm
P.S. If none of your friends know how to sort an array they shouldn't consider themselves programmers and you shouldn't be asking them for advice.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Aug 3 2018 08:16pm
The fastest sort depending on the min and max of the data set would probably be counting sort which is O(n) (in most cases).

Another alternitive would be radix sort which is another non comparitive sort. The key for integer sorting is to look for non comparitive sorting which is O(n).

Any sort that is comparitive will likely need multiple passes to sort data causing them to be like O(log n) and shit which as the data set grows the time it takes to compute the sorted array becomes longer.

Code
~ $ ruby sort.rb
Random array:
[2, 4, 5, 1, 8, 6, 3, 6]
Sorted array:
[1, 2, 3, 4, 5, 6, 6, 8]
Modify one instance of array:
[1, 2, 7, 4, 5, 6, 6, 8]
Sort this array:
[1, 2, 4, 5, 6, 6, 7, 8]

~ $ cat sort.rb
def counting_sort(array)
min = array.min
max = array.max

counts = Array.new(max-min+1, 0);
array.each { |n| counts[n-min] += 1 }

(0...counts.size).map { |i| [i + min] * counts[i] }.flatten
end

puts "Random array: "
p arr = [2,4,5,1,8,6,3,6]

puts "Sorted array: "
p arr2 = counting_sort(arr)

puts "Modify one instance of array: "
arr2[2] = 7
p arr2

puts "Sort this array: "
counting_sort(arr2)


This post was edited by AbDuCt on Aug 3 2018 08:20pm
Member
Posts: 5,000
Joined: Jul 4 2009
Gold: 118.00
Aug 4 2018 08:45am
Quote (waraholic @ 4 Aug 2018 02:41)
What language? Experienced developers don't write sorting algorithms, but you should learn and implemented a few in school. If this was java I'd just do:
Code
Arrays.sort(arr);


The sorting algorithm doesn't really matter when only sorting 100 integers. If you know about the data you're working with and you're going to run this sort a lot or for a large data set then you can implement a specific algorithm. Here is a reference I like: http://bigocheatsheet.com/

If you want to learn some sorting algorithms then I'd probably learn quicksort and bubble sort. They shouldn't take you longer than 10-15 minutes to understand.



Quote (waraholic @ 4 Aug 2018 02:42)
P.S. If none of your friends know how to sort an array they shouldn't consider themselves programmers and you shouldn't be asking them for advice.


Since it is just its algorithm, the language is not needed and specified.
I know I should know some sorting algorithm and I have learnt some, but I havent really used them since highschool.
The algorithm is matter even when it is only 100 integer, because they want to see if I know the basics.
And as you, they know how to sort array, but did not know which method was the best/fastest in this case.


Quote (AbDuCt @ 4 Aug 2018 03:16)
The fastest sort depending on the min and max of the data set would probably be counting sort which is O(n) (in most cases).

Another alternitive would be radix sort which is another non comparitive sort. The key for integer sorting is to look for non comparitive sorting which is O(n).

Any sort that is comparitive will likely need multiple passes to sort data causing them to be like O(log n) and shit which as the data set grows the time it takes to compute the sorted array becomes longer.


thank you, I will check them.
Member
Posts: 11,273
Joined: Apr 26 2008
Gold: 3,303.50
Aug 4 2018 01:30pm
Thought about this for a bit.
My first guess was to simply order with any O(nlogn) algorithm such as mergesort or quicksort or even O(n) with count/radix if you can.

I was thinking about a way to take advantage of the fact that only k elements are "out of order". If you can assume that you could insert an element in the array in constant time i was thinking about something like.
1) Find the highest(or lowest) element out of order -> This would take linear time
2) Insert the element in the "right" position -> You can even assume this is linear tbh.
3) Repeat 1-2 for k times (in this case 5)

Worst case scenario of this algorithm would be O(n^2) if k == n but in your problem it would be O(5 * n) == O(n).
I am generalizing the problem for n elements in the array and k changes. Depending on the relation between k and n you could choose to do either the sorting or my method.

If you want to discuss details i'd be happy to help ^^

P.S this was just my first go on the problem and lmk if i made a mistake.

This post was edited by xapeu on Aug 4 2018 01:31pm
Member
Posts: 2,123
Joined: May 6 2010
Gold: 824.00
Aug 4 2018 02:15pm
Quote (waraholic @ Aug 3 2018 06:42pm)
P.S. If none of your friends know how to sort an array they shouldn't consider themselves programmers and you shouldn't be asking them for advice.


This is a wrong mindset. Someone looking for a programming job and someone already working as a programmer is two completely different things, and algorithm-type questions are horrible measures of a person's ability to code or engineer. That is why so many companies are leaving this interview-type behind and focusing more on a candidates github and project experience.

That being said, I dont know how to sort an array...at the moment...because I dont spend my time learning how to sort an array (because im not in college anymore) , I spend my time writing code that inserts and processes thousands of data requests concurrently and at the fastest time possible, and i'm quite successful at it. But since I cant sort an array right now I guess I should just quit my fulltime well-paying job right?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 4 2018 11:19pm
Bubble Sort would be my goto answer. the key is to pick a sorting algorithm that performs faster when it's mostly sorted. in this case it's 5 * O(n), unlike a merge sort / quick sort.

there might be something better, but if faced in an interview this would be my first answer. if the interviewer is happy with it, he'll move on. otherwise he'll ask if you can come up with something better.

of course, you can modify it slightly to skip the already sorted part of the array earlier and start each iteration where the first swap happened

This post was edited by carteblanche on Aug 4 2018 11:21pm
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Aug 5 2018 11:19am
Quote (starknight17 @ Aug 4 2018 03:15pm)
This is a wrong mindset. Someone looking for a programming job and someone already working as a programmer is two completely different things, and algorithm-type questions are horrible measures of a person's ability to code or engineer. That is why so many companies are leaving this interview-type behind and focusing more on a candidates github and project experience.

That being said, I dont know how to sort an array...at the moment...because I dont spend my time learning how to sort an array (because im not in college anymore) , I spend my time writing code that inserts and processes thousands of data requests concurrently and at the fastest time possible, and i'm quite successful at it. But since I cant sort an array right now I guess I should just quit my fulltime well-paying job right?


Maybe it has to do with the fields we're working in, but as a full stack developer I have to sort data before presenting it most of the time even if that sort is just by adding `sort by x desc/asc` to my query or calling `sorted()` on the array/list/whatever. I've interviewed a lot of people and I would never ask this question. Writing an actual sorting algorithm isn't something most people need to do as a programmer and regurgitating something you learned in class doesn't mean you're a good problem solver.\

A lazy developer is a good developer. Don't reinvent the wheel. Sorting is the wheel.
Member
Posts: 11,273
Joined: Apr 26 2008
Gold: 3,303.50
Aug 6 2018 07:49am
Quote (carteblanche @ Aug 5 2018 05:19am)
Bubble Sort would be my goto answer. the key is to pick a sorting algorithm that performs faster when it's mostly sorted. in this case it's 5 * O(n), unlike a merge sort / quick sort.

there might be something better, but if faced in an interview this would be my first answer. if the interviewer is happy with it, he'll move on. otherwise he'll ask if you can come up with something better.

of course, you can modify it slightly to skip the already sorted part of the array earlier and start each iteration where the first swap happened



I think that bubble sort would not be O(n) in this case.
If you had [1, 2, 3, ..., n] in your array and changed the last element to a small one like [1, 2, 3, ..., n - 1, 0] the complexity would still be O(n^2) since you would need n iterations over the n elements to move the 0 backwards 1 position at a time.

Let me know if i missed something
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll