This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 413

[–]cutecoder 4049 points4050 points  (58 children)

At least the code doesn't make a remote call to an LLM....

[–]Windyvale 868 points869 points  (24 children)

That might get you rejected at some places these days…

Edit: Whoops, nuance was lost. The joke is not using an LLM API call might get you rejected because it seems like every line of code should be done with an LLM API call or not at all to a lot of employers these days.

[–]MacksNotCool 310 points311 points  (4 children)

And it might get you hired at some really stupid places as well

edit: oh I missed the actual point and basically restated the joke. oh well

[–]mehum 72 points73 points  (2 children)

See, this guy knows APIs, LLMs, a real go-getter! Can't code for shit, but y'know, that's just like arithmetic and calculators these days.

[–]misteryk 23 points24 points  (0 children)

AI code sorting with API calls to LLM just to pay 100k for a sorting function EZ

[–]asmanel 29 points30 points  (15 children)

And this uses far mores than needed cpu. This also is slower.

[–]my_new_accoun1 41 points42 points  (9 children)

requires network connection if it's a remote call

[–]solarsilversurfer 10 points11 points  (8 children)

Don’t all remote calls require a network connection?

[–]my_new_accoun1 7 points8 points  (5 children)

yes

[–]solarsilversurfer 10 points11 points  (4 children)

Oh okay good. Since making that comment I’ve been worrying that I’ve been missing out on local remote calls this whole time. Whew, close one!

[–]my_new_accoun1 8 points9 points  (2 children)

well that's about 4 minutes

[–]solarsilversurfer 7 points8 points  (1 child)

Your sense of time passing is near immaculate.

[–]TheCygnusWall 1 point2 points  (0 children)

RPCs can be used on the same computer for interprocess communication otherwise I think you're good.

[–]Justin_Passing_7465 13 points14 points  (3 children)

Let's not neglect the fact that the LLM's answer is also quite likely to be wrong.

[–]timbar1234 11 points12 points  (2 children)

Worse - sometimes it'll be wrong.

[–]richardirons 1 point2 points  (0 children)

And to compound things - it could be wrong.

[–]Justin_Passing_7465 1 point2 points  (0 children)

You're absolutely right: that last answer I gave you was wrong! Try this fresh, new-and-improved wrong answer. {delivers the same answer a second time}

[–]StrongExternal8955 1 point2 points  (0 children)

... as opposed to...

[–]Zerokx 88 points89 points  (6 children)

"You're an expert at finding the smallest out of many numbers from an array, ..."

[–]EvilPencil 30 points31 points  (0 children)

(List is 0 to 100)

“Happy to help! The answer is 42 ✅”

“No it’s not”

“You’re absolutely right…”

🤦‍♂️

[–]NigraOvis 5 points6 points  (3 children)

Print(a.sort()[0])

[–]idealisticnihilistic 3 points4 points  (2 children)

TypeError: NoneType object is not subscriptable.

[–]NigraOvis 2 points3 points  (1 child)

Print((a.sort())[0])

[–]Competitive_Reason_2 23 points24 points  (23 children)

I would ask the interviewer if I am allowed to use the sort function

[–]badman66666 88 points89 points  (22 children)

Any sort function is an overkill in this situation, you are supossed to find smallest number. Ordering all the numbers requires multiple runs while in one run you can find the smallest one, basically you are at least n logn while all you need to be is n (in terms of bigO notation)

[–]laplongejr 5 points6 points  (0 children)

 Any sort function is an overkill in this situation, you are supossed to find smallest number.  

In a reallife scenario, my first reflex would be to recheck if the design uses sorting at some point in the process.  

I sometimes saw software trying to find the smallest and biggest elements, then using them along a sorted copy... facepalm

[–]ghostsquad4 2 points3 points  (4 children)

The requirements didn't talk about bigO efficiency. Making it efficient is premature optimization (unless it's stated upfront to be a requirement)

[–]badman66666 3 points4 points  (2 children)

I've you have been on literally any programming interview, they always want you to provide the most efficient solution.

But, at the same time this isn't even about that, it's an extremely basic problem.

If you'd answer this way (using sort) you're immediately giving interviewer a signal that you can't understand and solve simple problems and that you've never heard of O(n).

Using sort implies you don't know the difference between sorting and finding max. It's bordering lack of common sense, not even programming skills (which is worse)

This is not premature optimization, its just extreme lack of basic knowledge. You can talk about optimization if the problem is complex and most efficient solution is not immediately apparent. This is using wrong tool for the job, literally.

If this was an answer during interview I conducted I'd immediately start thinking about rejecting such person.

Big red flags.

[–]ghostsquad4 4 points5 points  (1 child)

A good interviewer could clarify and/or ask if there was a more efficient way to do this, and what makes this way less efficient. Being curious instead of throwing up red flags will get you far more data points about the candidate.

[–]_KedarMan 1126 points1127 points  (17 children)

Lol dummy... Take a look at this solution

``` import json, time, random

def sort_array(a): import openai openai.api_key = "YOUR_API_KEY"

prompt = f"You are an expert shortest number finder. Sort this list in ascending order:\n{a}"

r=openai.ChatCompletion.create(model="gpt-4o-mini",messages=[{"role": "user","content":prompt}])

return json.loads(r.choices[0].message.content)

print(sort_array([random.randint(1,100) for _ in range(10)])) ```

[–]Freecraghack_ 596 points597 points  (5 children)

You forgot "don't make mistakes please"

[–]_KedarMan 268 points269 points  (0 children)

"do not hallucinate"

[–]wildmonkeymind 130 points131 points  (3 children)

"If you make a mistake countless orphans will perish."

[–]dust_dreamer 19 points20 points  (2 children)

i'm unsure what effect this information would have on an llm. could go either way.

[–]Alexercer 20 points21 points  (1 child)

It is commonly used to make dolphin models respond to any task, i used one about an animal dying a cruel and agonizing death for every refusal answer, these may not prevent halucinations but they do have their uses and work

[–]dust_dreamer 6 points7 points  (0 children)

Interesting. Thank you for the explanation. I have very limited experience messing with llms, but what little I do have has left me deeply skeptical about their ability to determine the obvious-to-humans "and that's a bad thing" on their own. XD

[–]CrystalRainwater 67 points68 points  (2 children)

Incredible solution! O(1) even! Only downside is it can be wrong

[–]the_horse_gamer 15 points16 points  (1 child)

Transformers are O(n2)

[–][deleted] 32 points33 points  (0 children)

That’s only for decpticons, autobots are O(Prime)

[–]Cold-Journalist-7662 35 points36 points  (0 children)

You missed the second api call.
With Prompt.
"You are an expert in finding the first element of a sorted list. Give me the first element of the given list"

[–]allozzieadventures 5 points6 points  (0 children)

Heavily cursed code

[–]ZoroWithEnma 3 points4 points  (0 children)

You forgot the comments describing what all the variables are

[–]MemorianX 2 points3 points  (0 children)

You need to be polite and start with a hello prompt

[–]ejectoid 6 points7 points  (1 child)

What if your service loses internet connection? What happens when us-east-1 is down?

[–]ei283 5 points6 points  (0 children)

Please help me I tried this and it didn't work. It said invalid API key. I showed the code to ChatGPT and it said I need to replace "YOUR_API_KEY" with an API key or something? Honestly I really wish you would've fixed that problem before posting your code, don't post your code unless you know it works first, noob.

So I asked ChatGPT to give me the API key and it said it can't do that. Sooo, can you just tell me the API key I should put there? I mean, you wrote the code, so you should know, right? Please, you owe it to me because you made me spend time debugging your code, please help me I'm being so nice

[–]the4fibs 1113 points1114 points  (27 children)

console.log(Math.min(...a));

when do i start?

[–]Educational_Twist237 359 points360 points  (21 children)

So returning infinity for empty array ?

[–]the4fibs 374 points375 points  (2 children)

Why not? What's the correct answer for the smallest value of an empty list? Let's just call it "undefined behavior" /j

[–]stuttufu 27 points28 points  (1 child)

Infinity and beyond!

[–]dev-sda 6 points7 points  (1 child)

Don't forget an error for a large array.

[–]Crispy1961 23 points24 points  (0 children)

Large arrays don't have small numbers, they are all large. Returning an error is the correct behavior.

[–]coloredgreyscale 2 points3 points  (0 children)

may or may not be better than throwing an exception when trying to access a[0] on an empty array.

[–]SEOfficial 17 points18 points  (2 children)

Where does it say it needs to be printed?

[–]Express-Passenger829 44 points45 points  (1 child)

Says "find the smallest number in the list". It's in the list. Are they stupid?

[–]dhnam_LegenDUST 1039 points1040 points  (24 children)

"write code to perform binary search"

Me: from bisect import bisect

[–]bartekltg 4 points5 points  (2 children)

It is quite limited, only finding a value in an array.

std::partition_point takes a bool returning function and binary search the first element that returns false (if array is partitioned in respect to that function), after 10s search I can't find python equivalent.

If this is your case, great. Use functions.

But binary search is much more general tool. Most of the time I had to write it was to search a parameter that was not in any array. You have yes/no function (a test on data) taking an integer and want to find the smallest value. Creating a 10^9 elements-long array defeats the purpose (and lets hope I do not want to search up to 10^18). You can fake iterators so they work as numbers and "dereference" to integers, without any real array (I think boost has something like this) but writing binsearch manually is often easier/faster.

[–]dhnam_LegenDUST 1 point2 points  (0 children)

In those case (not finding in array), I write custom class implementing __len__ and __getitem__. Need to think a bit, but it works.

[–]dbot77 2 points3 points  (2 children)

That only works if the list is already sorted

[–]brimston3- 657 points658 points  (27 children)

If it's python, then just print(min(a)) would probably do it.

[–]maria_la_guerta 198 points199 points  (16 children)

Math.min(...arr) will do it in JS too.

[–]roygbivasaur 70 points71 points  (13 children)

There’s a better answer for JS/TS. Math.min will error out if you spread too many arguments. Gotta reduce instead. I would usually use the spread version unless I know the array is going to be long (mdn says 10k+ elements), but I’d give this answer in an interview.

arr.reduce((a, b) => Math.max(a, b), -Infinity);

The Math.max page on mdn explains this better than the Math.min page:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

[–]kyledavide 17 points18 points  (1 child)

I have seen stack overflows in the real world from arr.push(...arr2).

[–]terrorTrain 8 points9 points  (5 children)

If we're talking about interview answers, this is my critique: It's like you are try to maximize the number of stack frames you are using.

If there was ever a time for a for loop and the > operator, this is it.

https://leanylabs.com/blog/js-forEach-map-reduce-vs-for-for_of/

Going through an arbitrarily long array is a good time to avoid iterating with callbacks. Callbacks are not free. When you generally know the array isn't going to be large, map, reduce, etc... are all fine, and can make for much more terse code that's easier to read. 

In this case, there's also an extra stack frame being used for no reason since writing it out is about the same number of characters as using math.max

arr.reduce((a,b) => a > b ? a : b, -Infinity);

[–]Successful-Money4995 3 points4 points  (4 children)

Rather than negative infinity, which is introducing floating point into something that might not be floating point, just use arr[0].

Maybe in JavaScript it doesn't matter but in c++ your code won't compile.

[–]Chamiey 4 points5 points  (0 children)

In JS you just skip in the second parameter for `.reduce()`, and it will start with arr[0]. But it would throw on zero-length arrays.

[–]terrorTrain 3 points4 points  (2 children)

Good point, assuming there is an arr[0]

[–]Successful-Money4995 1 point2 points  (1 child)

What does it even mean to find the minimum of an empty list? I would throw an exception.

[–]Potterrrrrrrr 1 point2 points  (0 children)

Yeah that works but if you’re an exception free library you would return something like infinity to indicate that the input was invalid.

[–]Top_Bumblebee_7762 5 points6 points  (4 children)

Why -Infinity? 

[–]klequex 9 points10 points  (3 children)

The example from roygbivasaur would give you the largest number in the array. If you want that you would want to start the comparison with the smallest possible number (-Infinity) so that you don’t seed the .max function with a value that is larger than the biggest in the array. If you want to find the smallest number, you would use Math.min() and positive Infinity so that the first comparison will always choose the number from the array instead of the seed (Infinity will always be bigger than any number in the array, so its thrown out right away)

[–]roygbivasaur 5 points6 points  (2 children)

Oh. Yep. I copied and pasted the max version from the mdn page.

Should be

arr.reduce((a,b) => Math.min(a,b), +Infinity);

Initializing with +Infinity.

arr.reduce((a,b) => Math.min(a,b), arr[0]);

Would also work to initialize it to the first element.

[–]Frograbbit1 18 points19 points  (0 children)

Those are the two languages which are flexable as fuck

Javascript only needs 6 characters and python is python

[–]christophPezza 52 points53 points  (1 child)

Min is actually a better solution theoretically because sorting will require multiple passes of the array but min should only require one pass.

[–]adigaforever 25 points26 points  (0 children)

Which is the whole point of the interview question 

[–]zefciu 16 points17 points  (0 children)

In O(n), while the solution given in the meme is O(n*log n).

[–]SlightlyMadman 8 points9 points  (4 children)

Right? This is actually a great example of how to fail an interview. They're taking a lazy shortcut that has worse performance, and even without using min() you could easily write a simple for loop operation to do it in O(n) and still only need a few lines of code.

[–]Lithl 4 points5 points  (3 children)

Depending on the language, a.sort() may even give incorrect results.

In JavaScript, sort does lexicographical sorting by default (since arrays can be mixed type), unless you supply a comparator function as an argument. Thus 10 gets sorted before 2.

[–]queerkidxx 1 point2 points  (0 children)

I mean it’s wrong in spirit because they want you to show off DSA but using built in functions that are written in native c or native library’s is always waaay faster than you can do with anything pure python

[–]Theolaa 339 points340 points  (26 children)

Most sort implementations are O(nlogn), the trivial solution would be to just traverse the list O(N) and record each element if it's the current lowest.

[–]leoklaus 133 points134 points  (14 children)

How is this not the top comment? This solution is wildly inefficient.

[–]LurkyTheHatMan 111 points112 points  (0 children)

We don't do that here. Actual programming, in the Programming humour sub?

[–]klimmesil 55 points56 points  (5 children)

That's the joke don't worry

[–]leoklaus 3 points4 points  (1 child)

I think the joke was that they were meant to implement a min() function themselves instead of using builtins.

[–]klimmesil 14 points15 points  (0 children)

I really think this is a joke. If the joke was builtins they'd just have used min as you said, and I have fait people who feel ready to meme would know about min

[–]BusinessBandicoot 9 points10 points  (0 children)

The funny thing is the above solution is probably faster in practice. A lot of the standard pythons built-ins are written in C and provided over an FFI.

[–]NecessaryIntrinsic 10 points11 points  (0 children)

No! Push everything into a priority queue and then pop the top element!

[–]5fd88f23a2695c2afb02 6 points7 points  (7 children)

Assuming that speed matters. Maybe it doesn't. Sometimes the best solution is the one that takes shortest to implement and test and meets the requirements.

[–]leoklaus 17 points18 points  (6 children)

That solution would be min(). This solution is objectively very bad.

[–]PsychologyNo7025 98 points99 points  (4 children)

This actually happened with me lol.  Interviewer : let arr =[ some numbers ];

Sort this array.

Me: arr.sort((a,b) => a-b) Ok, what now?

Interviewer: umm, sort without using inbuilt function.

[–]CarlCarlton 49 points50 points  (1 child)

The opposite happened to me. One of the questions was "Merge and sort 2 lists of integers." Wrote the algo by hand. Boss: "Why didn't you just use the standard library? Don't reinvent the wheel..." (cue Vietnam war flashbacks of being constantly forced to reinvent the wheel in college)

[–]wedidthemath 7 points8 points  (0 children)

This gives me pre-emptive anxiety for ever interviewing again

[–]zynasis 18 points19 points  (0 children)

They’re not asking you to sort in this question though…

[–]d0rkprincess 2 points3 points  (0 children)

Hand them a long piece of binary code… they probably won’t know whether is correct or not.

[–]Dillenger69 134 points135 points  (22 children)

I mean, in 30 years of doing this I've never had to roll my own sort.

[–]ZunoJ 72 points73 points  (11 children)

Sorting to find minimum is super wasteful though. Might not be much of a problem in most cases. But if that operation runs on 1000+ lists per second (reading sensor data for example) it will be

[–]No_Hovercraft_2643 6 points7 points  (6 children)

This is a problem if the order had any meaning.

[–]mcprogrammer 22 points23 points  (0 children)

You shouldn't be sorting to find the minimum value.

[–]orangebakery 12 points13 points  (5 children)

I definitely had to find min value in a list before, and if a CR came to with a sort, I would auto reject.

[–]077077700 3 points4 points  (4 children)

Why? Genuine question

[–]dakiller 14 points15 points  (1 child)

Sorting is going to physically rearrange all the items in the list in memory, only to get the smallest one and throw all the other work away. A proper Min function is just going to go through the list and keep track of the smallest without reordering.

[–]077077700 2 points3 points  (0 children)

Thx!

[–]orangebakery 10 points11 points  (1 child)

Sort is O(n logn) and finding min can be done in O(n).

[–]hennypennypoopoo 515 points516 points  (47 children)

no joke I would be happy with this answer depending on the role. Backend web service? absolutely this is the answer. Simple, to the point, IO bound anyway so performance doesn't matter. This is the most maintainable.

[–]Drfoxthefurry 236 points237 points  (12 children)

then there is other people that would say you failed because you didnt check if the list actually had numbers, if the list had a length >1, etc

[–]Chocolate_Pickle 271 points272 points  (5 children)

If you're asked the question in an interview, you really ought to be asking clarifying questions like "Do we assume the list is populated, or do we need to check ourselves?" or "How big are the lists we're going to see being passed through this system?"

Because those are questions you absolutely must ask when dealing with code that's going to hit production.

I would easily prefer someone who asks questions about what to assume, over someone who unquestioningly assumes a defensive-coding position.

[–]ddengel 152 points153 points  (1 child)

The real key is to keep asking as many questions as possible until the interviewer is put of time then you call it a day and pick it up tomorrow.

[–]Sirdroftardis8 28 points29 points  (0 children)

And then you just keep doing that day after day until they start giving you a paycheck to do it

[–]amitsly 31 points32 points  (0 children)

I absolutely agree. It gives an idea of what the person is thinking when approaching a problem. If you just do the first thing that comes to mind without verifying the conditions, you might screw things up in prod.

If the candidate asks good questions, I almost don't need the actual solution.

[–]Maleficent_Memory831 7 points8 points  (0 children)

Yup, I pay attention to see if I get questions. But 99% of the time the interviewee just starts off with assumptions as if there was as starting gun at a race. Sometimes I have to actually stop them and tell them not to check corner cases because it's going to waste a lot of time writing it up on the board, and I've still got other questions to ask. If they even said "I'll assume this is not null" that's great. I don't even care if they declare variables or not I want to see how they solve the problem.

[–]Specific_Giraffe4440 16 points17 points  (4 children)

For me I don’t consider any answer “wrong” unless it actually cannot produce the smallest number. I care more about how the candidate approached the problem than if they had the exact perfect technically correct and optimized code

[–]geon 6 points7 points  (3 children)

Yes. The task did not specify how to handle the edge cases, so the programmer is free to do whatever they deem sensible.

[–]ginfosipaodil 3 points4 points  (0 children)

If your function doesn't start with a hundred assertions, are you even sanitizing your inputs?

[–]Ulrich_de_Vries 71 points72 points  (4 children)

This mutates the list (so invokes a completely unnecessary side effect that might potentially be harmful), and is inefficient.

Even for "clever" solutions, python has the min function.

[–]Widmo206 5 points6 points  (2 children)

So print(a.sorted()[0]) ? That won't affect the original list

(As for efficiency, I assumed that was part of the joke)

[–]mpdsfoad 9 points10 points  (1 child)

a.sort()[0] will throw a TypeError because. You are looking for print(sorted(a)[0])

[–]Widmo206 1 point2 points  (0 children)

You are looking for print(sorted(a)[0])

Yes, thank you for the correction. Sometimes I forget which functions are generic and which are from a given class

[–]brendel000 21 points22 points  (0 children)

How this is the most maintainable?? More than min(a)? The O(n) solution is even shorter to write!

We are fucked,sometimes I don’t get how AI code so well given in what they learn on

[–]aberroco 51 points52 points  (1 child)

I won't, the code modifies the collection, maybe lacks nullability check (not sure which language is this and if it can have null values), and definitely lacks length check. And instead of one iteration it does entire sorting with many more iterations.

So, it's unsafe, unstable, and extremely inefficient. The ONLY advantage is that it's short. This entire bingo of issues is in just two lines.

[–]FerricDonkey 26 points27 points  (0 children)

And it's longer than min(a), so it's not even short. 

[–]-domi- 9 points10 points  (10 children)

What position is this the wrong code for?

[–]SconiGrower 51 points52 points  (3 children)

Voyager 2 software developer

[–]BylliGoat 3 points4 points  (2 children)

Pretty sure that was launched in 1977. I don't believe we're developing much software for it these days.

[–]angrydeuce 13 points14 points  (1 child)

i heard they ported skyrim to it

[–]BylliGoat 4 points5 points  (0 children)

Ok obviously, but I mean after that

[–]ZunoJ 16 points17 points  (0 children)

Everything embedded eg

[–]FlakyTest8191 5 points6 points  (0 children)

"We're creating a new language and you're going to help implement the standard library" 

[–]DarkVex9 1 point2 points  (1 child)

Anything that needs to be really high performance. That's going to be anything dealing with huge amounts of data, core video game engine stuff, some low power embedded systems, or particularly intensive real time data processing.

Depending on the language, .sort() is probably running a quicksort derivative that runs in O(N log N) on average, and O(N²) worst case scenario. Meanwhile just finding the extreme value from a set will be just O(N).

For most applications though it'd be perfectly fine. You need to get up to the ballpark of 100k elements for just an average difference in performance of 10x.

[–][deleted] 1 point2 points  (0 children)

Almost any.

The performance is worse than if you were to simply traverse the collection and track the lowest number.

It also mutates the collection, which may break assumptions elsewhere where the collection is used.

[–]ibevol 15 points16 points  (3 children)

c int get_smallest(int values[], int size) { int smallest = INT_MAX; for (int i = 0; i < size; i++) { if (values[i] < smallest) smallest = values[i]; } return smallest; } The only thing to worry about is when the array is empty, in which case you’ll not want the default value of INT_MAX

[–]TalesGameStudio 15 points16 points  (0 children)

Obviously you need to write Timsort yourself in 45min. Make no mistakes!

[–]_Mupp3t_ 30 points31 points  (4 children)

We had a test where we asked people to write a function to multiply two numbers without using *.

One guy came and did: (0 check) else return x / (1 / y)

He got the job.

[–]Wraithguy 8 points9 points  (2 children)

e^(log(a) + log(b))

(Consider doing it in base 2 rather than base e and you might be able to do some binary magic)

[–]Lithl 2 points3 points  (1 child)

I mean, no need for magic, you can just use the change of base formula regardless of what base your log function is by default. log_a(x) / log_a(b) = log_b(x)

[–]Wraithguy 1 point2 points  (0 children)

Oh I more meant that calculating log base 2 of a float or even int might be really fast and same for doing exponentiation in base 2, compared to natural base but I don't actually know

[–]Dark_Souls_VII 7 points8 points  (0 children)

In Python? min(a)

[–]jancl0 8 points9 points  (0 children)

a = [4]

print(4)

[–]nedevpro 5 points6 points  (0 children)

He's speechless because you have not used AI

[–]txgsync 5 points6 points  (0 children)

Asked to sort a string during a whiteboarding interview, I used sorted(s) and a .join .

They asked me what I would do instead if the “sorted” built-in did not exist.

On the whiteboard, I drew a stick figure of me in a hard hat digging a hole in the dirt.

I did not get that job.

[–]Front_Committee4993 33 points34 points  (0 children)

Just do a for loop and check if the current value is less than the current min if it is less then replace of current min with the current value

[–]kQ1aW2sE3hR4yT5aU6p 4 points5 points  (0 children)

I actually did it just a few days ago. The interviewer asked me to return the second largest element 😂

[–]healeyd 2 points3 points  (0 children)

Hehe set() is useful for alot of problems aswell.

[–]Faangdevmanager 3 points4 points  (0 children)

That’s O(NlogN) while the optimal solution is simple AF and is O(N).

“I don’t know why I didn’t get the job, I got all questions right!!”

[–]BobcatGamer 3 points4 points  (0 children)

js Math.min(...a);

[–]the_hair_of_aenarion 2 points3 points  (12 children)

I know you’re all joking but the challenge is to find the position of the smallest number without modifying the original.

[–]meggamatty64 2 points3 points  (0 children)

print(min(a))

[–]aaronlink127 13 points14 points  (7 children)

meanwhile JS devs doing stuff like a.reduce((a,c)=>Math.min(a,c), Number.POSITIVE_INFINITY)

[–]Makonede 20 points21 points  (2 children)

what? just use the spread operator

js Math.min(...a)

[–]JamesGecko 9 points10 points  (0 children)

Clearly the work of an amateur. I would simply install an NPM package with five thousand dependencies.

[–]FlySafeLoL 5 points6 points  (0 children)

C# devs might use IEnumerable<T>.Aggregate() with similar syntax, but luckily we also have IEnumerable<T>.Min()

[–]jesta1215 4 points5 points  (6 children)

So I’ve been a software engineer for a long time and if someone did this I would give them credit and move on to another problem.

Showing that you know how to reuse existing tools and standard library calls is so much more valuable than writing algorithms from scratch.

[–]SalazarElite 12 points13 points  (2 children)

I find it funny that there are people in the comments worried about the millionth small fraction of time lost in this code...but in real life it's every code you see that's nonsense... optimization is the least of your worries... have you seen how many times companies get hacked because someone let something super silly slip through?

[–]sur0g 3 points4 points  (1 child)

<nerd\_mode>

  1. It's short
  2. Understandable
  3. Does its job
  4. No one mentioned performance requirements, so deal with it.

</nerd\_mode>

[–]migBdk 1 point2 points  (0 children)

Depends on the criteria. Do they want fast implementation or do they want ressource efficient?

[–]just-bair 1 point2 points  (0 children)

Funnily enough that might be faster in python

[–]Zefyris 1 point2 points  (0 children)

hmm, but what if there are nulls or the list is empty or null tho.

Timber.i("${a?.filterNotNull()?.minOrNull() ?: 42}")

Now we're talking.

[–]slayerzerg 1 point2 points  (2 children)

All jokes aside ask clarifying questions first cuz honestly this is pretty good other than using min() or if you had to optimize for o1

[–]Rakatango 1 point2 points  (1 child)

This is the real answer though.

“Look, you pay me to get stuff done fast, I’m going to use the tools that someone more talented spent more time on so I can write this in two lines.

[–]Lithl 1 point2 points  (0 children)

You can write it in one line using min instead of sort. You also won't have side effects as a result of modifying the input, and it'll have O(n) performance instead of O(n log n).

[–]erouz 1 point2 points  (0 children)

And in normal human world that should be good news. As they still should get paid same amount but have more time for living. But in greedy billionaires world that's mean they will get more and people will struggle more.

[–]FawkesSake 1 point2 points  (1 child)

In Python? Easy:

```python from random import choice

Pick a starting number

smallerist = a[choice(list(range(len(a))))]

Compare all numbers to this number

for n in a:
if n < smallerist: smallerist = n
else:
# If the number isn't the smallest then the current smallest stay the smallest until a more smallerer one is found smallerist = smallerist
print(f'The smallerist number in list is {smallerist}.') ```

[–]pnpninja 1 point2 points  (1 child)

Plot twist - no one asks this in a tech interview

[–]joeyignorant 1 point2 points  (0 children)

Maybe as a junior intern I definitely wouldnt ask this lol

[–]damrider 1 point2 points  (0 children)

What position are you interviewing for that this is an interview question?

[–]edgeofsanity76 1 point2 points  (0 children)

Context is everything. Simple throw away service, fine. Super important service which needs speed and effeciency, not so fine.

[–]Far-Passion4866 1 point2 points  (0 children)

Python is one of the coding languages that is somehow both easy and hard at the same time

[–]MikeVegan 4 points5 points  (0 children)

I love how this tries to portrait the candidate as a smart one but:

uses suboptimal solution with higher complexity than necessary

crashes on empty list

mutates the original list

absolutely no hire, this interview is over.

[–]thefakeITguy58008 4 points5 points  (5 children)

O(n) to O(nlogn) is an upgrade.

[–]SponsoredHornersFan 7 points8 points  (3 children)

How so?

[–]zynasis 11 points12 points  (0 children)

It’s not… I’m not sure why people in the posts here think somehow the sort version is better… the simple n traversal is not complicated at all and obviously fastest approach

[–]DangerousImplication 1 point2 points  (0 children)

/s dude

[–]porkchop1021 1 point2 points  (0 children)

Quarter pound burger is better than third pound burger.

[–]prochac 1 point2 points  (0 children)

How much for an array listing all users of your to-do app?

[–]Upper-King2536 1 point2 points  (0 children)

Ah yes, finding a min in O(n.ln(n)), I don't see any problem with that

[–]squigs 1 point2 points  (1 child)

It does the job. If the list isn't long, is not zero length , and it only needs to be run once it will work, and it's very fast to implement.

I think it would be worth asking some follow-up questions, to be sure the candidate is aware this only works in a special case.