Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
public void run() throws IOException {
Map<String, Integer> finale = findFav(new File("/tmp/test"));
for (Entry<String, Integer> i : finale.entrySet()) {
System.out.println(i.getKey() + " " + i.getValue());
}
// this is the max count of color .
//System.out.println(Collections.max(finale.values()));
Set<Entry<String, Integer>> set = finale.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
// This is what is desired.
System.out.println(list.get(0).toString());
}
public Map<String, Integer> findFav(File fileName) throws IOException {
FileInputStream fis = new FileInputStream(fileName);
Map<String, Integer> colorCount = new HashMap<String, Integer>();
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
// Can add verifier for verifying the line is well written like
// favcolor=blue
String[] tmp = line.split("=");
String color = tmp[1];
Integer count = colorCount.get(color);
if (count == null) {
colorCount.put(color, 1);
} else
colorCount.put(color, ++count);
}
br.close();
return colorCount;
}
{
public void run() throws IOException {
Map<String, Integer> finale = findFav(new File("/tmp/test"));
for (Entry<String, Integer> i : finale.entrySet()) {
System.out.println(i.getKey() + " " + i.getValue());
}
// this is the max count of color .
//System.out.println(Collections.max(finale.values()));
Set<Entry<String, Integer>> set = finale.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
// This is what is desired.
System.out.println(list.get(0).toString());
}
public Map<String, Integer> findFav(File fileName) throws IOException {
FileInputStream fis = new FileInputStream(fileName);
Map<String, Integer> colorCount = new HashMap<String, Integer>();
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
// Can add verifier for verifying the line is well written like
// favcolor=blue
String[] tmp = line.split("=");
String color = tmp[1];
Integer count = colorCount.get(color);
if (count == null) {
colorCount.put(color, 1);
} else
colorCount.put(color, ++count);
}
br.close();
return colorCount;
}
}
Here is my solution:
public class FavoriteColor {
public static void print(String strPath) {
Map<String, Integer> map = new HashMap<>();
try(BufferedReader reader = Files.newBufferedReader(Paths.get(strPath))) {
String s;
while((s = reader.readLine()) != null) {
String fileColor = s.substring(s.indexOf("=") + 1);
if (map.containsKey(fileColor)) {
int oldCnt = map.get(fileColor);
map.put(fileColor, oldCnt + 1);
} else {
map.put(fileColor, 1);
}
}
} catch(IOException e) {
System.err.println("IOException occured.");
e.printStackTrace();
return;
}
ValueComparator comp = new ValueComparator(map);
Map<String, Integer> sortedMap = new TreeMap<String, Integer>(comp);
sortedMap.putAll(map);
List<String> result = new ArrayList<>();
if (sortedMap.size() != 0) {
int maxCnt = sortedMap.values().iterator().next();
for (Map.Entry<String, Integer> e : sortedMap.entrySet()) {
if (e.getValue() == maxCnt) {
System.out.println(e.getKey() + ": " + e.getValue());
}
}
}
}
private static class ValueComparator implements Comparator<String> {
private Map<String, Integer> map;
public ValueComparator(Map<String, Integer> map) {
this.map = map;
}
@Override
public int compare(String s1, String s2) {
if (map.get(s1) >= map.get(s2)) {
return -1;
}
return 1;
}
}
}
1) Use your preferred file i/o classes and libraries to read in the file. 2) Ignore "Name:"; find the token "favcolor=". 3) Read the string which follows it (worth clarifying how the file is formatted, i.e. is it white space separated). 4) If that string has not already been read, add it to your favourite map/ dictionary container of string, int, where the string is the key and int is the number of times you've seen that string in the file. 5) If it already exists in the container, find that key and increment its value. 6) Store the first string, int pair as "mostFavoriteColor". 7) If after incrementing a key it has a value which is greater than the value in mostFavoriteColor, set "mostFavoriteColor" = the more favored key, value. 8) When you've finished reading the file, print out mostFavoriteColor.
- merlinme November 06, 2014