Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 1 vote

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 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Read each line , split it with delimiter '=' , we will get the color , maintain a hashmap of color , keep on increasing the count. At the end print , color and count.

- praveenkcs28 November 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

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;

}

- Swt November 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
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;

}
}

- Swt November 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@swt I have a question? What code line allows you to input your favorite color?

- KH December 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
		}
	}
}

- Anton December 26, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More