VMware Interview Question: Given a series of strings, fi... | Glassdoor

## Interview Question

Member of Technical Staff Software Engineer Interview Palo Alto, CA

# Given a series of strings, find the biggest common prefix.

Tags:
string logic

1

/**
* Jun Zheng, Rice Univ
* Given a series of strings, find the biggest common prefix.
* Real question of VMware
* Java7; running time: O(n^2)
* @param str
* @return
*/
private String biggestPrefix(String[] strs){
String prefix=strs[0];
for(int i=1;i0;i++){
int j;
for(j=0;j0)? prefix:&quot;No such prefix!&quot;;
}

Jun Zheng on Feb 20, 2013
0

How that works?
You are considering &quot;prefix&quot; must come from the very first string, which is not true.

In the following string set, the biggest common prefix is &quot;xyzasd&quot; - which this program fails to find!

String[] arr = {&quot;MxyzasdNmm&quot;, &quot;kxyzasdDodal&quot;, &quot;I am a Good Boy&quot;, &quot;JadxyznasdM Golmal&quot;, &quot;ABCDEF&quot;, &quot;ABCDEFGH&quot;, &quot;Sunnyvale&quot;, &quot;CaliforniaKxyzaszzMon&quot;};

Java User on Feb 20, 2013
2

What? Prefix is not started from the very first string? Jesus I cannot read Eng!

Jun Zheng on Feb 21, 2013
0

x = [&quot;Ravite&quot;, &quot;Raviteja&quot;, &quot;Ravby&quot;, &quot;Raviejaain&quot;,&quot;Ravi&quot; ]
#Given String
y = x[0]
for i in x:
if len(i)&gt; len(y):
y = i
fin = []
y = list(y)
for i in range(len(y)):
count = 0
for j in range(len(x)):
if y[i]==x[j][i]:
count +=1

if count == len(x):
fin.append(y[i])
else:
break
if len(fin) &gt; 0:
print &quot;&quot;.join(fin)
else:
print &quot;No common prefix in series&quot;

Raviteja Ainampudi on Jan 23, 2017