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
Answer

Interview Answer

4 Answers

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:"No such prefix!";
    }

Jun Zheng on Feb 20, 2013
0

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

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

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

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 = ["Ravite", "Raviteja", "Ravby", "Raviejaain","Ravi" ]
#Given String
y = x[0]
for i in x:
    if len(i)> 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) > 0:
    print "".join(fin)
else:
    print "No common prefix in series"

Raviteja Ainampudi on Jan 23, 2017

Add Answers or Comments

To comment on this, Sign In or Sign Up.