Google 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

Solution in python

import re

def numVowels(line):
    count = 0
    for c in line:
        if re.search("[aeiou]", c):
            count += 1

    return count

def bestScoreLine(fileName, scoreFunction):
    f = open(fileName)

    bestLine = None
    bestScore = None

    for line in f:
        curScore = scoreFunction(line)

        if bestLine is None:
            bestLine = line
            bestScore = curScore
        elif curScore > bestScore:
            bestLine = line
            bestScore= curScore

    return bestLine    

## MAIN
print bestScoreLine("test.txt", numVowels)
print bestScoreLine("test.txt", len)

test.txt

abcdef
ab
abcdefghik
abcdefghkhllj
aeiouseiou

OUTPUT

aeiouseiou

abcdefghkhllj

- whatevva' December 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

why use regular expressions instead of something like
if c in ['a', 'e', 'i', 'o', 'u']:
?

- Miguel Oliveira December 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in java:

public interface Criteria {

	public Score getScore(String input);
}

public class VowelCriteria implements Criteria {

	@Override
	public Score getScore(String input) {
		int score = 0;
		for(char c : input.toCharArray()) {
			if(isVowel(c)) {
				score++;
			}
		}
		return new Score(score, input);
	}

	private boolean isVowel(char c) {
		return c == 'a' ||
				c == 'e' ||
				c == 'i' ||
				c == 'o' ||
				c == 'u';
	}
}

public class LengthCriteria implements Criteria {

	@Override
	public Score getScore(String input) {
		int score = 0;
		if(input != null) {
			score = input.length();
		}
		return new Score(score, input);
	}
}

public class Score implements Comparable<Score> {

	public int score;
	public String input;

	public Score(int score, String input) {
		super();
		this.score = score;
		this.input = input;
	}

	@Override
	public int compareTo(Score s) {
		return Integer.valueOf(this.score).compareTo(s.score);
	}
}

public class CriteriaCalculator {

	public static String[] words = new String[]{"georgia", "seattle", "arizona", "texas", "atlanta"};
	
	public static Score getMaxScore(String[] words, Criteria c) {
		Score maxScore = null;
		for(String word : words) {
			Score s = c.getScore(word);
			if(maxScore == null || maxScore.compareTo(s) <= 0) {
				maxScore = s;
			}
		}
		return maxScore;
	}

	public static void main(String[] args) {
		Score vowelMax = getMaxScore(words, new VowelCriteria());
		System.out.println(vowelMax.input);
		System.out.println(vowelMax.score);

		Score lengthMax = getMaxScore(words, new LengthCriteria());
		System.out.println(lengthMax.input);
		System.out.println(lengthMax.score);
	}
}

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

Java code using Pattern and Matcher class.

public static void main(String args[])throws IOException{
		File file = new File("Input.txt");
		
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String str = null,finalStr=null;
			int count=0,finalCnt=-1;
			while((str = br.readLine()) != null){
				count=0;
				Pattern p = Pattern.compile("[aeiouAEIOU]");
				Matcher m = p.matcher(str);
				while(m.find()){
					count++;
				}
				if(count>finalCnt){
					finalCnt = count;
					finalStr = str;
				}
			}	
			
			System.out.println("Max vowel string :: "+finalStr);
			System.out.println("Max vowel count :: "+finalCnt);
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

- Coder December 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If we can work on distributed environment we can also use MapReduce algo to improve performance.

- thelineofcode December 18, 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