Interview Question for Java Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
0
of 2 vote

What is wrong with this answer:

public static List<String> fizzbuzz(int start, int end, Map<Integer, String> lookups) {
		List<String> fizzBuzzList = new ArrayList();
		Set<Map.Entry<Integer, String>> entries = lookups.entrySet();
		  
		for (int i = start; i<=end;i++){				
			StringBuffer fbBuffer = new StringBuffer();		
			fbBuffer.append(i + " : ");
			for (Entry<Integer, String> entry : entries){		
				if ( i % entry.getKey() == 0){
					fbBuffer.append(entry.getValue());						
					}				
			}
			fizzBuzzList.add(fbBuffer.toString());
		}
			
		return fizzBuzzList;		
	}

- java.strat October 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does Map<K,V> return the entries in sorted order of the keys? If not, then you will get the wrong answer.

- Anonymous October 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I ran/tested your code and it seemed to work fine, not sure why you were downvoted.

- JonG October 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tested and works.

/*Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz".
	For numbers which are multiples of both three and five print "FizzBuzz".

	Additionally, instead of printing "Fizz" or "Buzz", create a lookup such that 3 --> "Fizz", 5 --> "Buzz", 7 --> "Woof" and so on. The signature of the method would be:

	List<String> fizzbuzz(int start, int end, Map<Integer, String> lookups) { ..}

	The expected output is of the format : 15:FizzBuzz, 21:FizzWoof, 105: FizzBuzzWoof, etc*/
	public static void fizzBuzzStuffs() {
		boolean is3 = false, is5 = false, is7 = false;
		String out = "";
		for (int i = 0; i < 100; i++) {
			is3 = isMultOf(i, 3);
			is5 = isMultOf(i, 5);
			is7 = isMultOf(i, 7);
			out = "";
			
			if (!is3 && !is5 && !is7) {
				System.out.println(i);
			} else {
				out = "";
				if (is3){out += "Fizz";	}
				if (is5){out += "Buzz";	}
				if (is7){out += "Wolf";	}
				System.out.println(out);
			}
		}
	}
	
	public static boolean isMultOf(int val, int mult) {
		return val%mult == 0;
	}

- JonG October 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oops, I didn't read the instruction well - will rewrite.

- JonG October 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static void main(String[] args) {
		Map<Integer, String> lookups = new HashMap<Integer, String>();
		lookups.put(3, "Fizz");
		lookups.put(5, "Buzz");
		lookups.put(7, "Wolf");
		
		fizzbuzz(-10, 120, lookups);
	}
	
	
	public static List<String> fizzbuzz(int start, int end, Map<Integer, String> lookups) {
		List<String> list = new ArrayList<String>(end-start+1);
		Set<Entry<Integer, String>> entrySet = lookups.entrySet();
		
		
		String out = "";
		for (int i = start; i < end+1; i++) {
			out = i+": ";
			
			for (Entry<Integer, String> entry: entrySet) {
				if (isMultOf(i, entry.getKey())){
					out+= entry.getValue();
				}
			}
			list.add(i-start, out);
			System.out.println(out);//TODO for testing purposes.
		}
		return list;
	} 
	
	public static boolean isMultOf(int val, int mult) {
		return val%mult == 0;
	}

- JonG October 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks for the ans without using stringbuffer. It also covers tests using negative integers and 0.

- Anonymous October 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Regarding whether the HashMap returns sorted order of keys, one cannot guarantee the order with HashMaps anyway, unless you use a LinkedHashMap. As long as the required value is displayed, its acceptable I guess.

- Anonymous October 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i=0;i<n;i++)
{
if(a[i]%(15)==0)
  cout<<"FIZZBUZZ"<<endl;
else if(a[i]%3==0)
  cout<<"FIZZ"<<endl;
else if(a[i]%5==0)
  cout<<"BUZZ"<<endl;
}

- Anonymous October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Print1to10 {
public static void main(String[] args) {
int count = 1;
while (count < 100) {
{
{
if(count % 3 == 0)
System.out.print("FIZZBUZZ");

else

if(count % 5 == 0)
System.out.print("FIZZ");


}
}
System.out.print(count + " ");
count++;
}
}
}

- pRASHANT March 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

good ans

- Anonymous October 16, 2013 | 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