Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

public static String getLongestSubstringWithoutRepeatingCharacters(String str){
        if (str == null || str.isEmpty()){
            return null;
        }
        HashSet<Character> presented = new HashSet<>();
        int maxLength = 0, resultHead = 0;
        for (int tail = 0, head = 0 ; tail < str.length(); tail++){
            while(presented.contains(str.charAt(tail))){
                presented.remove(str.charAt(head++));
            }
            presented.add(str.charAt(tail));
            if (presented.size() > maxLength){
                resultHead = head;
                maxLength = presented.size();
            }
        }
        return str.substring(resultHead , resultHead+maxLength);
    }

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

Use the sliding window mechanism

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

sliding window

public String lengthOfLongestSubstring(String s) {
	    int max = 0 ;
	    int tail = 0 ;
	    String candidate = "";
	    HashMap<Character, Integer> map = new HashMap<> ();
	    for (int i = 0 ; i < s.length() ; ++i) {
	    	if (!map.containsKey(s.charAt(i))) {
	    		map.put(s.charAt(i), 1) ;
	    	} else{
	    		map.put(s.charAt(i), map.get(s.charAt(i)) + 1) ;
	    		while (map.get(s.charAt(i)) != 1) {
	    			map.put(s.charAt(tail), map.get(s.charAt(tail)) - 1) ;
	    			tail++;
	    		}
	    	}
	    	if (max < i - tail + 1) {
	    		max = i - tail  + 1 ;
	    		candidate = s.substring(tail, i + 1) ;
	    	}
	    	
	    }
	    return candidate ;

}

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

#include<stdio.h>
#include<string.h>
int main()
{
char str[30],i=0,hash[256]={0},count_max=0;
printf("enter the string\n");
scanf("%s",str);
int count=0;
int k=strlen(str);
while(i<k)
{
while(hash[str[i]]==0&&i<k)
{
hash[str[i]]+=1;
count++;
i++;
}
if(count>count_max)
{
count_max=count;
//memset(hash,0,sizeof(hash));
}
else{
count=0;
memset(hash,0,sizeof(hash));
}

}
printf("%d",count_max);
return 0;
}

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

std::string LongestSubStr(std::string s)
{
   std::string longest;
   std::vector<bool> dupCheck(256, false);
   for (int i = 0,j; i < s.size(); i++)
   {
      for (j = i; j < s.size() && !dupCheck[s[j]]; j++)
         dupCheck[s[j]] = true;
      if (j - i > longest.size())
         longest.assign(&s[i], j-i);
      dupCheck.assign(256, false);
   }
   return longest;
}

void main()
{
   char* tests[] = {"PrincessFart", "FunnyGuy", "abba", "abbcbab", "SomeRandomString"};
   for (int i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
      printf("%s->%s\n", tests[i], LongestSubStr(tests[i]).c_str());
   getch();
}

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

output:

PrincessFart->Princes
FunnyGuy->nyGu
abba->ab
abbcbab->cba
SomeRandomString->eRandomStri

- tjcbs2 March 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class test {


public static void main(String[] args) {
String s = "hellohjghfrrrbghjklioopu";
int maxLength = 0;
String MaxString = "";

for (int i = 0; i < s.length(); i++) {
for (int j = 1; j <= s.length(); j++) {
if(i<j){
String subStr = s.substring(i,j);
char[] subArr = subStr.toCharArray();

if (chkDuplicate(subArr)){
if(s.substring(i, j-1).length() > maxLength){
maxLength = s.substring(i, j-1).length();
MaxString = s.substring(i, j-1);
}
i=j-1;
}
}
}
}

System.out.println(MaxString);
}

public static boolean chkDuplicate(char[] subArr){

for (int i = 0; i < subArr.length; i++) {
for (int j = i+1; j < subArr.length; j++) {
if(subArr[i] == subArr[j])
return true;
}
}
return false;
}
}

- Aditya March 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class test {


public static void main(String[] args) {
String s = "hellohjghfrrrbghjklioopu";
int maxLength = 0;
String MaxString = "";

for (int i = 0; i < s.length(); i++) {
for (int j = 1; j <= s.length(); j++) {
if(i<j){
String subStr = s.substring(i,j);
char[] subArr = subStr.toCharArray();

if (chkDuplicate(subArr)){
if(s.substring(i, j-1).length() > maxLength){
maxLength = s.substring(i, j-1).length();
MaxString = s.substring(i, j-1);
}
i=j-1;
}
}
}
}

System.out.println(MaxString);
}

public static boolean chkDuplicate(char[] subArr){

for (int i = 0; i < subArr.length; i++) {
for (int j = i+1; j < subArr.length; j++) {
if(subArr[i] == subArr[j])
return true;
}
}
return false;
}
}

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

public class test {
	
	
	public static void main(String[] args) {
		String s = "hellohjghfrrrbghjklioopu";
		int maxLength = 0;
		String MaxString = "";
		
		for (int i = 0; i < s.length(); i++) {
			for (int j = 1; j <= s.length(); j++) {
				if(i<j){
				String subStr = s.substring(i,j);
				char[] subArr = subStr.toCharArray();

					if (chkDuplicate(subArr)){
						if(s.substring(i, j-1).length() > maxLength){
						maxLength = s.substring(i, j-1).length();
						MaxString = s.substring(i, j-1);
						}
						i=j-1;					
					}
				}
			}			
		}
		
		System.out.println(MaxString);
	}
	
	public static boolean chkDuplicate(char[] subArr){
		
		for (int i = 0; i < subArr.length; i++) {
			for (int j = i+1; j < subArr.length; j++) {
				if(subArr[i] == subArr[j])
					return true;				
			}			
		}
		return false;		
	}
}

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

public class test {
	
	
	public static void main(String[] args) {
		String s = "hellohjghfrrrbghjklioopu";
		int maxLength = 0;
		String MaxString = "";
		
		for (int i = 0; i < s.length(); i++) {
			for (int j = 1; j <= s.length(); j++) {
				if(i<j){
				String subStr = s.substring(i,j);
				char[] subArr = subStr.toCharArray();

					if (chkDuplicate(subArr)){
						if(s.substring(i, j-1).length() > maxLength){
						maxLength = s.substring(i, j-1).length();
						MaxString = s.substring(i, j-1);
						}
						i=j-1;					
					}
				}
			}			
		}
		
		System.out.println(MaxString);
	}
	
	public static boolean chkDuplicate(char[] subArr){
		
		for (int i = 0; i < subArr.length; i++) {
			for (int j = i+1; j < subArr.length; j++) {
				if(subArr[i] == subArr[j])
					return true;				
			}			
		}
		return false;		
	}
}

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

/*

        Find longest unique substring.

        usage:

        % ./a.out string

        This prints *first* instance of longest unique string. Iterates once
        through the string, setting a bit in an array of masks if it sees
        a letter that it hasn't seen before.  If it HAS seen it before,
        it first sets the position of the repeated letter as the beginning of a new
        "candidate" lus and clears the mask, which if it becomes longer than
        the previous lus, becomes the new lus.

        change '>' to '>='

                if (++lus_candidate_len >= lus_len) {

        to get last.

        Notes:

        Adds a total of 61 bytes to the stack with printfs commented out.

        ./a.out "abcdabcde"

        ./a.out "abcdeabcd"

*/

#include <stdio.h>
#include <stdlib.h>

main(int argc, char **argv) {
        char *s;
        unsigned long bits[4] = {0,0,0,0};
        unsigned long set = 0;
        unsigned char i,c;
        register int curr;
        int lus_candidate, lus;
        int lus_candidate_len, lus_len;
        if (argc == 2) {
                s = argv[1];
        } else {
                printf("Nothing to do.\n");
                exit(0);
        }
        lus_candidate = lus = curr = 0;
        lus_candidate_len = lus_len = 0;
        printf("string: %s\n",s);
        for(curr=0; curr<strlen(s); curr++) {
                c = (unsigned char)s[curr];
                i = c >> 5;
                set = 0x01<<(c & 0x1F);
                if (bits[i] & set) {
                        do {
                                c = (unsigned char)s[lus_candidate];
                                bits[c >> 5] ^= 0x01<<(c & 0x1F);
                                lus_candidate_len--;
                        } while(s[lus_candidate++] != s[curr]);
                }
                bits[i] |= set;
                if (++lus_candidate_len >= lus_len) {
                        lus = lus_candidate;
                        lus_len = lus_candidate_len;
                }
        }
        if (lus_len) {
                printf("lus   %s%*s%.*s\n\"%.*s\" at offset %d with length=%d.\n",
                        lus==0 ? ":":": ",lus," ",lus_len,&s[lus],
                        lus_len,&s[lus],lus,(int)lus_len);
        } else {
                printf("Nothing found.\n");
        }
}

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

I think this would fail for input like
babcd
It would return bcd instead of abcd

- tjcbs2 March 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@tjcbs2: right you are: let me see if I can salvage it!

That oughtta do it.

- commodius_vicus March 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Code formatter does't like '>>' operator. >>;5 should obviously be >>5

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

Python implementation

def longestSub (inp):
    maxlen=0
    st=0
    ret=""
    chmap=dict()
    for i in xrange(len(inp)):
        if (inp[i] not in chmap.keys()):
            #first time seeing this character
            chmap[inp[i]]=1
        else:
            chmap[inp[i]]+=1
            #we need to advance the start of
            #substring until we have unique
            #characters between st and i+1
            while (chmap[inp[i]] != 1):
                chmap[inp[st]]-=1
                st+=1

        #we have one substring with unique chars
        #check if this is longest so far
        if (maxlen < i-st+1):
            maxlen = i-st+1
            ret=inp[st:i+1]

    return (ret)

- rravir November 22, 2016 | 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