Interview Question


Country: United States




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

public class ContinousString {

public static void main(String args [])
{
String str ="asdfihjasdabcdefhsdjfaabcdefghijfkas";
String constr = "";
String finalst="";
int i;
for(i=0;i<(str.length()-1);i++){
if(str.charAt(i)+1==((str.charAt(i+1)+1)-1)){
constr+=str.charAt(i);
if(finalst.length()<=constr.length()){
finalst=constr;
finalst+=str.charAt(i+1);
}
}

else{
constr="";
}

}
System.out.println(finalst);
}
}

- sourabhmehta83 March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String data = "abcdefsdbcdefghmnxyz";
        StringBuffer result = new StringBuffer();
        StringBuffer temp = new StringBuffer();
        for(int i=0; i< data.length(); i++){
            int startIdx = i;
            while((i+1< data.length()) && (data.charAt(i)+1 == data.charAt(i+1))){
                i++;
            }
            if(startIdx != i){
                temp.append(data.substring(startIdx, i+1));
                if(temp.length() > result.length()){
                    result = temp;
                }
                temp = new StringBuffer();
            }
        }
        System.out.println(result);

- Sat March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String longestConsecutiveString(String target){
		int tail = 0 , max = 0 ;
		String rst = "" ;
		for (int i = 1 ; i < target.length() ; ++i) {
			if (target.charAt(i) - target.charAt(i -1) != 1) {
				if (i - tail > max) {
					max = i - tail ;
					rst = target.substring(tail, i) ;
				}
				tail = i ;
			}
		}
		return rst ;

}

- Scott March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def conletters(s):
    str1 = ""
    finalstr = ""
    for i in range(len(s)-1):
        if ord(s[i])==ord(s[i+1])-1:
            str1 = str1+s[i]
            if (len(str1)>len(finalstr)):
                finalstr = str1
                finalstr = finalstr+s[i+1]
        else:
            str1 = ""
    print(finalstr)

conletters("abcdefghijk")

- SivaShanky March 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java solution:

public String findLongestAlphabet(String s){
	final int n = s.length();
    
    StringBuilder builder = new StringBuilder();
    builder.append(s.charAt(0));
    
    String ans = builder.toString();
    
    for (int i = 1; i < n; i++){
    	if (s.charAt(i) != s.charAt(i - 1) + 1){
        	builder.setLength(0);
        }
        
        builder.append(s.charAt(i));
        
        if (builder.length() > ans.length()){
        	ans = builder.toString();
        }
    }
    
    return ans;
}

- Inucoder March 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def find_longest (inp_string):
    start = 0
    max_count = 0
    max_start = 0 
    count = 0
    i = 1
    while i < len(inp_string):
        
        print (inp_string[i])
        if ( ord(inp_string[i-1]) == ord(inp_string[i])-1):
            count = count + 1
            if (count > max_count):
                max_count = count
                max_start = start
        else:
            count = 0
            start = i
            
        i = i + 1
    print (inp_string[max_start:max_start+max_count+1])

- TinkerBell April 29, 2015 | 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