Java Interview Question
April 5, 2016
I don’t do Java — I don’t have enough fingers to type so much — so I’ll write my solution in Awk, which is perfect for data-laundry problems like this one:
BEGIN { FS = "|" } ! ($2 in students) { students[$2] = $1 scores[$2] = $3 } $2 in students { if ($1 < students[$2]) { students[$2] = $1 scores[$2] = $3 } } END { for (s in scores) print s, scores[s] }
The BEGIN action sets the field delimiter, the first comparison saves the minimal student id and score for a new course, the second comparison resets the minimal student id and score for an existing course when a new minimal student id is found, and the END action prints the result. Assuming the program shown above is written in a file scores.awk
, you can run the program from a command prompt like this:
$ awk -f scores.awk input.txt Math 45 English 81
The order of the output is non-deterministic. You can run the program at http://ideone.com/FCg9cI.
Perfect problem for perl to solve as it’s extraction/parsing….
@josejuan: nice, not seen Haskell written like that. I’m puzzling over what ‘↪-↱_’ might mean though.
Fear my leet Julia skills (just kidding). Storing the lowest-numbered student’s score for each course (what a weird request, by the way). There are Pair datatypes for that and the min function has an appropriate method out of the box :)
And I replaced the course names just to see that character encodings still work.
Python:
@matthew I’m using enomsg/vim-haskellConcealPlus, yes, it make code less readable (if not use these symbols) but it’s nice if you are accustomed :) (symbols mapping here https://github.com/enomsg/vim-haskellConcealPlus/blob/master/after/syntax/haskell.vim )
There needs to be a Scheme version. This is whatever version of Guile they had on the server (rather old actually). The hash tables, string-split, command-line and some module system with a read-line in it are Guile. The solution itself is the same that I already wrote in Julia.
It seems to deal with my UTF-8 input and output. Good.
@josejuan: Thanks for that, interesting. I thought this looked like a good problem for map reduce, I’m not too keen on Java, but here’s a solution in Javascript & node.js:
This was just begging to be solved in R:
data <- read.csv(file = "~/Desktop/input.csv", sep="|", header = FALSE);
aggregate(by = as.data.frame(data[,2]), as.data.frame(data[,3]), min);
Might as well add an awk version:
Pure sort version (GNU coreutils) 8.4. (So the output is just the relevant lines from the file and still needs formatting, can’t do that in sort.)
Well, we haven’t had a C++ version yet, so here is one. Needs C++14 (specifically auto for lambda parameters). Assumes course ids are non-zero.
file= open(“input.txt”,”r”)
dict = {}
for line in file:
cuurent = line.split(“|”)
if cuurent[1] not in dict:
dict[cuurent[1]]=int(cuurent[0])
elif dict[cuurent[1]] > int(cuurent[0]):
dict[cuurent[1]]= int(cuurent[0])
print dict
Oh ok, a Java solution, output sorted by course name.
@r.clayton: isn’t that just showing the lowest score, rather than the score of the lowest numbered student?
Yup. I either misread the problem (most likely), or twisted it around in my head by the time I got to writing that part of the code. I guess I don’t get that job.
A Java solution redux.
Java solution using the stream API: https://gist.github.com/gdejohn/91e26d20a95fb2c16d778a908ab5f173