ifItWorksItWorks

Mar 31, 2025 7:42 PM

typeof

Views

26882

Likes

327

Dislikes

12

lol

programmer_jokes

programmerhumor

funny

meme

Well nobody said the list was dynamic so if it’s just defined like that you don’t need to use “code” to find the smallest value, you can just use your eyes.

4 months ago | Likes 1 Dislikes 0

4 months ago | Likes 23 Dislikes 0

Even easier is: console.log(Math.min(...a));

4 months ago | Likes 3 Dislikes 0

4 months ago | Likes 3 Dislikes 0

"Our first step is to re-define the concept of counting from first principles starting with the definition of the set" - Cantor, probably

4 months ago | Likes 3 Dislikes 0

But what if the code should sort it according to user input and not a predetermined list.

4 months ago | Likes 3 Dislikes 1

Jitter, take the wheel!

4 months ago | Likes 4 Dislikes 0

not wrong though.

4 months ago | Likes 9 Dislikes 4

Yes it is, it's doing an alphabetical sort. It is only correct if the data input is correct, otherwise 10, 100, 1000 etc will come before 2

4 months ago | Likes 5 Dislikes 0

If you're the one writing the list, why not just make it sorted to begin with?

4 months ago | Likes 5 Dislikes 1

That takes 4 years of college, and usually a graduate degree to learn to type: "ORDER BY"

4 months ago | Likes 1 Dislikes 0

It's likely that the built-in sort is significantly faster than a linear scan in the high level language. So - this is not only correct, it's probably faster.

4 months ago | Likes 10 Dislikes 2

if you discover a sorting algorithm that is provably better than O(n), you will win a Nobel Prize.

4 months ago | Likes 2 Dislikes 0

But it's not just algorithm, it;s implementation.

A hand crafted machine code sort can be bloody fast compared to a high level language.

And - speed of execution isn't the only speed there is. Speed of development can be very important. Spending an hour writing code you'll use once and takes 1 second to execute is inferior to spending 5 minutes and taking 2 seconds to execute.

4 months ago | Likes 1 Dislikes 0

It's not only wrong though

4 months ago | Likes 3 Dislikes 0

It works for the given dataset, but yes - you'd want to make that a numeric sort.

4 months ago | Likes 1 Dislikes 1

Queeeeeeeeeeeeeeeeeeee

4 months ago | Likes 1 Dislikes 0

This is the interviewer's fuckup for poor requirements. Code performs as requested. Issues from applying the code outside design parameters are a design fault, not a bug.
That said, I'd be far more likely to hire this person for QA than development, as their mind clearly appreciates a maze of twisty passages, all likely to cause either race conditions or implicit assumption faults.

4 months ago | Likes 1 Dislikes 0

Yeah, I had an interview that was basically an off my 1 error, fired up the debugger and talked/walked tough it, fixed it, and they never called me back.

4 months ago | Likes 1 Dislikes 0

I'd be tempted to send them a bill for my consulting fee.
They might even pay it without realizing what it was for.

4 months ago | Likes 1 Dislikes 0

Few things wrong here...
(1) JavaScript sorts alphabetically, so this fails with mixed length numbers e.g. [6, 3, 60, 100] => [100, 3, 6, 60]
(2) Answer has a poor Big-O and is slow due to sorting
(3) Answer could be shorter and more readable `Math.min(...a)`

4 months ago | Likes 5 Dislikes 4

To sort numerically, you want `myArray.sort((a, b) => a - b)`. Otherwise JS defaults to alphabetically. You need the `(a, b) => a - b` comparison function.

> The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

4 months ago | Likes 3 Dislikes 1

Yeah, Math.min() is definitely the better answer here. Why bother sorting the whole list?

4 months ago | Likes 2 Dislikes 0

I've seen worse code. Main issue here is my point (1), where the sort won't give you the right answer.

`[ 5, 1000, 4, 9 ].sort()[0]` will return 1000, due to JS .sort() default casting to strings and comparing the UTF-16 codepoint values.
> The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

4 months ago | Likes 1 Dislikes 1

It is SO fucking stupid when they ask you to write code to do something that has literally existed as built in functionality in programming languages for over a quarter century.

4 months ago | Likes 2 Dislikes 0

4 months ago | Likes 1 Dislikes 0

```
var a = [6, 2, 3, 8, 1, 4];
console.log(Math.min(...a));
```

4 months ago | Likes 2 Dislikes 0

This code is not efficient. It's simple, yes, but it's slower than a good solution. The optimal solution is O(n) and this is O(n*log(n)). It also takes more memory.

4 months ago | Likes 1 Dislikes 0

shut up nerd

4 months ago | Likes 2 Dislikes 0

4 months ago | Likes 1 Dislikes 0

I know a lot of you will hate this
var a = [6,2,3,8,1,4];
console.log(a[a.reduce((x, y) => x + y, 0) / a.length]);

4 months ago | Likes 2 Dislikes 0

4 months ago | Likes 3 Dislikes 0

In case someomes wondering, a real solution isn't that much longer. For example:

Var a = [6, 2, 3, 8, 1, 4], m = a[0], i = 1;
while (i < a.length) if (a[i] < m) m = a[i]; i++;
console.log(m);

I'm sure there's a more efficient way but this should get the job done pretty straight forward no overhead.

4 months ago | Likes 1 Dislikes 0

x.Where(entry => entry.Value => min);

4 months ago | Likes 1 Dislikes 0

var a = [4, 2, 6, 9, 4, 2, 5];
var smallest = a[0];

for (var i = 1; i < a.length; i++) {
if (a[i] < smallest) {
smallest = a[i];
}
}

console.log(smallest);

I think that gives better big O complexity. But the meme solution does work and is one of those waste of time coding questions these horrible tech interviews love to use.

4 months ago | Likes 22 Dislikes 1

put the number 10 or 100 in that list and you will see it does not work ;p

4 months ago | Likes 2 Dislikes 2

Math.min(...a)

4 months ago | Likes 2 Dislikes 0

Also the meme's answer does not work, except for the given example. JavaScript sorts alphabetically, so this fails with mixed length numbers e.g. [6,3,60,100] => [100, 3, 6, 60].

4 months ago | Likes 5 Dislikes 1

[deleted]

[deleted]

4 months ago (deleted Apr 1, 2025 12:08 AM) | Likes 0 Dislikes 0

Nope. Try my example above. Default is casting to a string then comparing the UTF-16 codepoints.

4 months ago | Likes 3 Dislikes 1

To sort numerically, you want `myArray.sort((a, b) => a - b)`. Otherwise JS defaults to alphabetically. You need the `(a, b) => a - b` comparison function.

> The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

4 months ago | Likes 2 Dislikes 0

Smallest = a[1] is quicker, but may give the wrong answer occasionally

4 months ago | Likes 6 Dislikes 0

Your answer is actually far superior since theirs will also be wrong with the number 10 or 100 etc in the list since it is alphabetical sort
so both will be wrong depending on data input

4 months ago | Likes 4 Dislikes 0

Your solution - while correct - is likely slower than the built-in sort, surprisingly enough. The system library sort routines in most languages have been heavily optimized for speed - especially for a small list.

4 months ago | Likes 12 Dislikes 0

For large arrays (~1 million elements), sorting will generally be slower than a simple loop because of the O(n log n) complexity.

4 months ago | Likes 2 Dislikes 0

The built-in solution can't possibly be faster than looking at each element in a list and doing one comparison for each of them.

4 months ago | Likes 4 Dislikes 3

It can if the under lying sort calls into fast as fuck machine code rather that interpreted code

4 months ago | Likes 4 Dislikes 1

It's gonna depend on a few things - your javascript engine, array size, and maybe how random the data is.

I did javascript in the 90's - the built-ins were orders of magnitude faster. That's less true today. The correct answer is to profile the code if speed is an issue.

4 months ago | Likes 2 Dislikes 0

It always depends, and profiling will need to be done to validate and verify. I do think it's relatively safe to say using built-ins is likely faster -nl not just performance but readability and overall developer experience

4 months ago | Likes 1 Dislikes 0

A lot of students just had their first lesson in programming, huh?

4 months ago | Likes 2 Dislikes 0

I'm just here trying to teach em. stop thinking you're clever, you're not.

4 months ago | Likes 1 Dislikes 0

They wanted code not math. This is what they asked for.

4 months ago | Likes 5 Dislikes 2

What's the joke here?

4 months ago | Likes 63 Dislikes 0

Basically that the purpose of the question is to demonstrate that you can recreate the inner workings of the kind of comparison algorithm to perform the work. Instead pre-built functionality of a language is being used to arrive at the end-result that creating it yourself would.

4 months ago | Likes 7 Dislikes 0

Which is, in itself a dumb as fuck request which leads to people actually thinking they can write better procedures (hint: no they fucking don't)to do things like find and replace and sort. And then Decades later some shmuck like me has to constantly rinse their eyes with eyebleach and get addicted to pain killers because that super find and replace is an goddamn infinite loop in the right circumstances, the right circumstances being someone tried to find and replace...

4 months ago | Likes 4 Dislikes 0

There are three different skills at play, and unfortunately many only recognize the first two. You need to know what the language and environment can offer built in, and leverage that. You need to know the underlying fundamentals of how basic algorithms work. AND you need to know when to apply each.

Certainly one should avoid reinventing the wheel. But if you don't know the inner workings of 'the wheel' it might never occur to you that the code you are about to write is just a modified wheel.

4 months ago | Likes 3 Dislikes 0

Coding equivalent of being asked ‘Write a formula where x equals 3’ and answering ‘x = 3’

4 months ago | Likes 96 Dislikes 4

worse, it's like "find X" and drawing an arrow instead of making calculations

4 months ago | Likes 27 Dislikes 7

It's really not. The answer given is terrible as it is slow. There are much better solutions. If a candidate ever gave me this as an answer during an interview, this would be the point where I'd start asking them about what games they've been playing since they're definitely not getting the job.

4 months ago | Likes 1 Dislikes 1

Few things...
(1) JavaScript sorts alphabetically, so this fails with mixed length numbers e.g. [6,3,60,100] => [100, 3, 6, 60]
(2) Answer has a poor Big-O and is slow due to sorting
(3) Answer could be shorter and more readable `Math.min(...a)`.

4 months ago | Likes 37 Dislikes 5

I should have also called out:
(4) poor test case—negatives, positives, zero, -0, floats, NaN, large numbers, BigInts, int32array, ArrayBuffer, Infinity, non-numbers like null and objects; and not asking the interviewer clarification questions if these matter and what they mean by "number"
(5) no input validation
(6) sort mutates the input array
(7) using `var` instead of `let` or `const`

4 months ago | Likes 1 Dislikes 0

Big O you say?

4 months ago | Likes 3 Dislikes 0

.sort() sorts alphabetically by default, you can pass in your own evaluation function to make it sort in whatever manner you please

4 months ago | Likes 2 Dislikes 0

You are correct, these people jumping to conclusions instead of taking literally 5 seconds to test it. I did it for them

4 months ago | Likes 19 Dislikes 2

Still found the smallest number.

4 months ago | Likes 1 Dislikes 1

tldr; I'm right, but unconvincing.

4 months ago | Likes 9 Dislikes 1

picture speaks 1000 words! however your post went from -5 to 0 now so there is still hope

4 months ago | Likes 6 Dislikes 0

Thank you for the breakdown!

4 months ago | Likes 2 Dislikes 0

No, JS interprets these as numbers. Alphabetical sort is only in effect when you put the numbers within quotation marks.

4 months ago | Likes 8 Dislikes 7

Sadly no. JS is a...well, it's a language. Try it yourself https://playcode.io/javascript "var a = [6,3,60,100];a.sort();console.log(a)"

4 months ago | Likes 2 Dislikes 0

you expect too much, no it doesn't

4 months ago | Likes 11 Dislikes 1

That's so unimaginably shitty that I can't even begin to articulate how much I hate it. I never want to see this again.

4 months ago | Likes 3 Dislikes 0

holy fuck, what a clusterfuck of a language

4 months ago | Likes 2 Dislikes 0

It's using alphabetical sort, all languages default sort is this exact type of sort. The only real difference here is javascript doesn't tell you that. But honestly I do not know of a language that has a default sort that isn't the same.

4 months ago | Likes 1 Dislikes 0

Nope. You want `myArray.sort((a, b) => a - b)` to sort numerically. Otherwise JS defaults to alphabetically. You need the `(a, b) => a - b` comparison function.

> The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

4 months ago | Likes 8 Dislikes 1

Tested and confirmed:

4 months ago | Likes 4 Dislikes 0

Is that method better than hard typing the array to int32? I don't deal with JS ever so the lack of typing is weird to me but this guarantees your input is always an integer sort right?
var a = new Int32Array([3, 1, 6, 100]);
a.sort();
console.log(a[0])

4 months ago | Likes 2 Dislikes 0

Interviewer asked to find the smallest "number", which you'd have to ask if that means Int32. Otherwise valid numbers in JS include: floats, Infinity, -0, NaN. Plus interviewer may want BigInts. Weird corner cases to handle like is -0 smaller than 0? Should non-numbers be excluded (input validation)?

4 months ago | Likes 2 Dislikes 0

Your first point is interesting, wasn't aware of that!

Another point is that even if the sorting algo did work, you're only asked to find the lowest value, not sort the list, so rearranging it could fuck up potentially useful data since we aren't given the context for the variable.

4 months ago | Likes 2 Dislikes 0

(2) is not necessarily a poor Big-O. most implementation of Array.sort is merge/insertion sort which is O(n log n). But also proven to be a good compromise on speed/memory for real world data

4 months ago | Likes 6 Dislikes 1

You can do in O(N) by checking if each element is the new lowest seen value. Any comparison-sort also needs to do at least N comparisons (generally far more). You'll want to stick to built-in functions, like `Math.min.apply(null, array)`.

4 months ago | Likes 6 Dislikes 2

print(min(a))

4 months ago | Likes 72 Dislikes 0

Right answer, wrong language.

4 months ago | Likes 3 Dislikes 6

Math.min(...a)

4 months ago | Likes 5 Dislikes 0

Uncaught ReferenceError: min is not defined

4 months ago | Likes 2 Dislikes 0

generally speaking, this is correct. Too many coders try to be overly clever. However, leaning on existing functions like min() allows your code to benefit from future framework efficiency gains.

4 months ago | Likes 35 Dislikes 1

I do SQL more than anything, and I am just floored at some of the complexity people put together with usually much simpler solutions

4 months ago | Likes 16 Dislikes 0

floor(FartsSmellBad) /s

4 months ago | Likes 2 Dislikes 0

"an exception occurred" (another programmer overloaded min() )

4 months ago | Likes 10 Dislikes 1

"You guys are working in a language that allows overloading min()? Why is your company 30 years behind the curve?"

4 months ago | Likes 4 Dislikes 0

python

4 months ago | Likes 2 Dislikes 0

Since when does python allow overloading built-in functions? That wasn't an option the last time I worked on it a few years back.

4 months ago | Likes 1 Dislikes 0

since always, python is dumb, but good for some things.

4 months ago | Likes 2 Dislikes 0

4 months ago | Likes 1 Dislikes 0

My favourite so far was a way to sort a list: a.forEach(el => setTimeout(() => console.log(el), el) )

4 months ago | Likes 143 Dislikes 1

4 months ago | Likes 1 Dislikes 0

I tried with signed number and to my surprise, I got the answer even before I ran the code!

4 months ago | Likes 9 Dislikes 0

I was feeling down after seeing some bad code today. This made me feel better.

4 months ago | Likes 3 Dislikes 0

.

4 months ago | Likes 1 Dislikes 0

You forgot to return just the lowest value, to fully answer the interview question. Also, thank you for your time. You can head home now; you can skip the rest of the interviews. We'll be in touch.

4 months ago | Likes 1 Dislikes 0

I understand what that does, but what language is this?
variable a is the list
for each element in a (set a timeout equal to that element (print the element to the screen) after the timeout)

4 months ago | Likes 3 Dislikes 0

JavaScript

4 months ago | Likes 5 Dislikes 0

I need to use this somewhere...

4 months ago | Likes 3 Dislikes 0

you can even add some complexity to it to help you actually capture the lowest value

function least(list) {
return Promise.race(list.map((e) => new Promise((resolve) => setTimeout(resolve, e, e))));
}

4 months ago | Likes 2 Dislikes 0

Thank you! I had never seen this before, but it got me thinking. I whipped it together in Java as a multithreaded example using sleep and I'm going to present it to my students along with some discussion questions like: What's the time complexity? In practice, what factor dominates the runtime? Is this a joke, if so, why? Are there circumstances under which this would be a good sorting algorithm? And so on, so genuinely THANK YOU, I think this makes a great learning example.

4 months ago | Likes 1 Dislikes 0

Wow. Impressively creative

4 months ago | Likes 1 Dislikes 0

Should have sent a poet!

4 months ago | Likes 3 Dislikes 0

The best sorting algorithm is Stalin Sort and has a run time of O(n). It states that for each element in the list, if such element is not already sorted, it is eliminated from the list.

4 months ago | Likes 28 Dislikes 1

but i neeeded the 2 datastruct...?

4 months ago | Likes 2 Dislikes 0

4 months ago | Likes 12 Dislikes 0

Sleep sort. One of the easiest sorting algorithms out there.

4 months ago | Likes 36 Dislikes 0

And one that scales O(n). Take that, "quick"sort.

4 months ago | Likes 17 Dislikes 1

You're assuming the thread scheduler operates in constant time.

4 months ago | Likes 1 Dislikes 0

I always asked myself.. still O(n) if you start N processes and count X times, with X the number? isn't it a nested for cycle? or sleeping is O(1)?

4 months ago | Likes 2 Dislikes 0

4 months ago | Likes 2 Dislikes 0

I think it would actually be O(n^2). If you sleep for 1s times the value of each item, it will break if it takes more than one second to assign all those timeout callbacks, [2,3,4,1] might return 2,1,3,4 if the last callback is set a second after the first. Iterating the list that way is O(n), which is the lower bound for how long to sleep, so the overall algo is O(n^2). It's just hidden because the test cases are so tiny, that it acts more like O(n) with a huge K.

4 months ago | Likes 8 Dislikes 0

There's also the oddity that it scales based on the values. Sorting [4,3,2,1] takes much less time than sorting [104,103,102,101]. Big-O notation doesn't cover that.

4 months ago | Likes 6 Dislikes 0

the order is linear n, it's just a very high unit of n

4 months ago | Likes 2 Dislikes 0

Wait WHY DOES THIS WORK

4 months ago | Likes 11 Dislikes 2

well.. it won't if the numbers are too close together and there are too many of them. It depends more and more on your actual computer hardware and the order of them being set into the sleep at that point lmao. Mystery outcome sort

4 months ago | Likes 3 Dislikes 1

It tells the computer to wait x time them print x. So you give it 5, 1, 10. In 1 second, it prints 1, in 5 seconds it prints 5, and in 10 seconds, it prints 10. What you see on the console is 1, 5, 10.

4 months ago | Likes 30 Dislikes 0

Yes, but in this case it's milliseconds so you probably wouldn't even notice unless the numbers go past 500

4 months ago | Likes 1 Dislikes 0

4 months ago | Likes 1 Dislikes 0

Nah it works, as it'll be queued to the call stack in the order from smallest to largest timeout

https://imgur.com/BKVn1DB

4 months ago | Likes 2 Dislikes 0

I know it will work, but setting the timeout at 500 is half a second so it might be noticeable then. If your system can run the code and update the screen in less than a millisecond, then it will be start to be noticeable at some point, maybe not exactly at 500, but at some point, yes

4 months ago | Likes 1 Dislikes 0

OH COME ON

4 months ago | Likes 10 Dislikes 1

if someone did this in an interview, i'd give them a stern talking to about how javascript is horrible and then probably recommend hiring them.

4 months ago | Likes 2 Dislikes 0

I just figured it out and wow.

4 months ago | Likes 4 Dislikes 1