Ebay Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

// for simplicity assume all characters are in lower case
public class FormWordsFromChemistryPeriodicTable {
	
	public static List<String> findWords (String[] dictionary, HashSet<String> periodicTableElements) {

		List<String> words = new LinkedList<String>();
		
		if(dictionary == null || periodicTableElements == null 
				|| periodicTableElements.isEmpty() || dictionary.length ==0)
			return words;
		
		for (int i = 0; i < dictionary.length; i++) {
			
			boolean flag = true;
			
			for (int j = 0; j < dictionary[i].length(); j++) {
				// if current letter is an element symbol, continue loop; else check its neighbhours 
				if (! periodicTableElements.contains("" + dictionary[i].charAt(j))) {				
					
					// if current letter is not an element symbol 
					// check if the prev letter + this letter is a symbol, 
					// else; check if this letter + next letter is a symbol
					if ( ! (
							(j != 0) && 
							periodicTableElements.contains("" + dictionary[i].charAt(j-1) + dictionary[i].charAt(j)))) {
						
						if ((j == dictionary[i].length()-1) || 
								! periodicTableElements.contains("" + dictionary[i].charAt(j) + dictionary[i].charAt(j+1))) {
							flag = false;
							break;
						}	
						
						j=j+1;
					}
				}
			}
			
			if(flag == true) {
				words.add(dictionary[i]);
			}
		}
		
		return words;
		
	}
	
	public static void main(String[] args) {
		String[] dictionary = {"ark","sick", "arc", "arch", "arche"};
        HashSet<String> periodicTableElements = new HashSet<String>();
        periodicTableElements.add("ar");
        periodicTableElements.add("k");
        periodicTableElements.add("si");
        periodicTableElements.add("c");
        periodicTableElements.add("ch");
        System.out.println(findWords (dictionary, periodicTableElements));
	}

}

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

Hash all the symbols from Chemistry Periodic table. For any given words, use DP to test whether the word can be formed by the hash table. dp[i][j]: whether the sub sequence word[0,...,i-1] can be created.
dp[i][j] = dp[i][j-1] word[i] in the hash table or
dp[i][j-2] word[i-1,i] in the hash table.

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

I could not assert - will this algo consider combining more than 2 symbols and in every possible order, also?

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

@ruharish I'm sorry. I thought all elements in the Chemistry Periodic table contains at most 2 letters. There are a few containing 3 letter. I should update the algorithm to loop back at most 3 letters(containing itself), However, the methods are similar.

- notbad October 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is similar to reconstruct a sentence without space between words.
This one is easier, since we know the length of a word is at most 3.

Using array to record the computed values, the algorithm can be done in O(n)

- StrongTSQ October 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

We can do this by hashing all the words of periodic table,
for comparing [AKBaBr] we can make a bool array(exist) of size 6 , with each exist[i] denotes word from 0 to i exist or not.

Suppose at time t, we arrive at [true,true,false,true,unknown,unknown] , now to fill exist[4] check (exist[3]==true and word[4] is in hash table) Or (exist[2]==true and word[4,5] is in hash table.) ... as both are false so exist[5]=false.

for exist[6] check (exist[4]==true and word[5] is in hash table) Or (exist[3]==true and word[5,6] is in hash table.) as 2nd condition is true .. therefore exist[6] is also true. return(exist[6]);

- Not important November 09, 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