This is a Java solution to the Insight code challenge on finding political donors. The source code includes 6 classes:
- Driver.java : this class includes the main method. It deals with the input read logic and pass valid item to corresponding handler.
- MedianByZip.java : this class is responsible for calculating median by zip and outputing
medianvals_by_zip.txt
. HashMap is used to store the mapping between (recipient, zip) and (median, number of transactions, total amount). An inner class has been created to wrap recipient and zip together as the key type of the map. The value type (median, number of transactions, total amount) is an instance of MedianVals class. A method to write to output file is invoked each time a streamed in record has been handled by this object. - MedianByDate.java : this class is responsible for calculating median by date and outputing
medianvals_by_date.txt
. TreeMap is used to store the mapping between (recipient, date) and (median, number of transactions, total amount). A customized comparator has been passed to the TreeMap to ensure all records will be sorted alphabetical by recipient and then chronologically by date. An inner class has been created to wrap recipient and date together as the key type of the map. The value type (median, number of transactions, total amount) is an instance of MedianVals class. Results will be written to destination file after all records from input file have been taken care of. - MedianVals.java : this class wraps median, number of transactions, total amount and a MedianHeap object as the value type in map (both HashMap and TreeMap). Note that an instance of MedianHeap is used to keep track of the stream median.
- MedianHeap.java : this is the lower level implementation of keeping track of stream median. The basic idea is to combine a max heap and a min heap to create a median heap. More about the principle can be found here.
- Tokenize.java : this is a customized StringTokenizer. The built-in StringTokenizer doesn't suit our need well when two delimiters are adjcent. We subclass the StringTokenizer to return "" instead of null in situations like this.