Java Interview Question
April 5, 2016
We have an interview question today:
Input comes from a file containing pipe-delimited records with three fields: student id (a positive integer), course title (a string), and score (a positive integer). You may assume that any combination of student id and course is unique. Here’s an example input file:
22|Math|45 23|English|52 22|English|51 26|Math|72 23|Math|61 21|English|81The file may have any number of records, and there is no limit on the number of unique courses. You should write a program to read the file and write a list of all courses in the file, combined with the score of the lowest-numbered student in the course. Thus, the correct output for the input shown above is:
Math 45 English 81
Your task is to write a program to solve the interview question; the original question specified a Java solution, but you are free to use any language. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
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