9 ms·
I’m right here, bombing Leetcode interviews because they ask me to write a program that sorts the columns in a CSV file in 15 minutes and I only know C.
by Unklejoe 4y ago
I’m right here, bombing Leetcode interviews because they ask me to write a program that sorts the columns in a CSV file in 15 minutes and I only know C.
- ericbarrett 4y agoLikewise. I've known C since the 90s, C++ since the 00s (with a refresher for C++11), and recently picked up Rust—have already written a few thousand lines, it's a great language. But I do DevOps because the pay is better and the interviews are practical.
- deleted 4y ago[deleted]
- yolo69420 4y agoMaybe I'm missing something but unless the interviewer expects a 100% correct csv parser that handles all possible corner cases this is basically - read file - start at beginning; if you encounter separator, add new entry to an array. if you encounter newline, save entire current array as a new line to an array of lines. - do lexicographical sort of arrays (can just use qsort for this). - if the test q actually wants you to parse out numbers and shit like that, nothing about the structure changes, you just have to record some extra info when parsing values and then use a different comparison depending on the type in the function you pass to qsort I dare say someone programming algorithms in a kernel or writing code for custom file systems should be able to do this. OK 15 min may be tight if you're nervous but you should get there.
- ericbarrett 4y agoYou forgot quotes. Unfortunately we're moving forward with other candidates at this time. Good luck in your job search!
- the_only_law 4y agoIt gets very nasty at this point especially because I’ve used popular languages who’s CSV parsing libraries were unable to handle some weird quotes properly.
- mandelbrotwurst 4y agoWhat languages? No option to configure any character as quote character or delimiter?
- d4mi3n 4y agoRuby. Python. Several JavaScript NPM packages. Part of the problem here is the standards around CSVs aren’t consistent, nor are they well respected. Things get even weirder when you throw non-Latin characters in the mix. May such parsers predate widespread use of UTF-8.
- hedora 4y agoRFC 4180!
- the_only_law 4y agoPython I believe was the issue in my case.
- motoboi 4y agoState machine is all you need.
- ummonk 4y agoI don’t think you realize how much harder this becomes with C. You probably need to can’t just add strings to an array you’ll need to allocate space for all the arrays (presumably by doing a first pass to determine CSV dimensions) and all the substrings (since they’re Cstrings). Personally I’d do it by storing indexes into the CSV contents instead of using null terminated C strings. Doing all of this in just 15 minutes is going to be hard. By the way, your solution as described is wrong, since it sorts each row independently. What you’d need to do is store all the data in a transposed format i.e an array of pointers to column arrays, then sort the array by giving qsort a comparison function that looks at the first entry in each column.
- deleted 4y ago[deleted]
- alkaloid 4y agoHoly moly, I thought I was the only one! "Want me to write a specialised API with custom encapsulation transmitted over UDP?" "Nah fam, we want to watch you code in our super-duper-wiz-bangy-exclusive IDE that you've never used before. Oh, and you have an hour. Oh, and it's going to be super-specific tasks that only we know how to do." "So, no on the encapsulation, eh?" <click>
- pjc50 4y agoI'd be tempted to just use sort(1) in that situation and say "what? it's in C" if questioned.
- AnimalMuppet 4y agoI'd actually hire you for that. You want to not waste time reinventing the wheel? Cool! But then, when I interview, I don't ask leetcode questions in the first place...
- ummonk 4y agoThis is missing the point of such questions. They aren’t aimed at asking “how would you solve this easy task?”. What they’re checking is “since we can’t have you demonstrate how you would solve a complex task in the short time allotted to an interview, let’s see how you can solve a smaller pretend task.”
- flukus 4y agoIn practice for this particular question they're asking if you've memorized sort algorithms. I have not (or I've forgotten since) and I doubt even the brightest minds of the industry that did invent them did so within an hour long job interview.
- hansvm 4y agoWas that (implement sort) the main point of the question? Especially given the 15 minute time window this sounds more like some kind of fizzbuzz-style question (here's a basic idea, can you translate it to code somehow in some language?). Usually when interviewers give me that kind of thing they're literally fine with bubble-sort (which I'd hope anyone could code on the fly), or even just assuming some appropriately shaped sort function exists and writing the rest of the code around it.
- ummonk 4y agoNo that's not what the question is asking. You should be using the standard library sort function (or making one up if you can't remember the specific API).
- ungamedplayer 4y agoBrother !
- alexfromapex 4y agoThis must be common, I just bombed a leetcode (tm) interview where I was asked to write code to share a method between two structs using traits in Rust without duplicating code. I couldn't remember a time I'd ever seen it done with generics so I tried to find it in the documentation but nothing. I looked up the solution after and it is not possible without writing a macro which is metaprogramming and seems like a poor way to assess how I'd perform writing Rust on a daily basis when I only had ~20 minutes to do it and was transparent that I am still learning the more advanced concepts. There were other red flags I ignored in the interview process, I look at it as a bullet dodged.
- deleted 4y ago[deleted]
- Brentward 4y agoCan't you just provide a default implementation of the method on the trait itself? Unless there are more requirements I'm not parsing from what you wrote, that seems to do exactly what they wanted.
- saghm 4y agoYeah, I'm not sure I fully understand as well. The only thing I can think of is that the trait was supposed to be from outside the crate (i.e. not modifyable), but then then, I'm not sure why writing a function that was generic over T: ThatTrait that took an instance as a parameter (or even `impl ThatTrait`) wouldn't work
- throwaway1777 4y agoThat is not really a leetcode-style interview question. Leetcode focuses on DSA questions not language knowledge.
- ummonk 4y agoWhat they’re describing is leetcode style problems that require the coder to use an abstract high level language that makes coding easy. What you’re describing is the opposite - some kind of esoteric question related to a specific language.
- nonameiguess 4y agoThe real answer here is take whatever the R data.table package is doing and copy that. It's even written in C. It's by far the best csv reading package I've ever used and will sort as it reads, sometimes extremely fast since it can pretty intelligently guess if a radix sort is possible. The only downside is I believe it can only handle csvs that fit in memory, but memory is getting pretty damn big these days. I used it as inspiration a long while back when I needed to write a library to read in ARPA-formatted n-gram language models, which is a fairly obscure file format I could not find any existing libraries for. Of course, data.table has existed for over a decade and you're not going to come up with something comparable in 15 minutes, but that's why you'd want to just copy it and not come up with your own solution in the first place.