Motorola Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

input: input string
1. keep a set with all characters in a window [s, e]
2. start with s = 0
3. increment e as long as input[e] not already in set
[s, e) is now a candiate, maximize it
now increment s until input[s] = input[e] and remove each input[s] from the set
go to 3

runs in O(n) with O(n) space for result

def find_longest_substring_wo_repeating_char(input):
    n = len(input)
    s = 0
    e = 0
    chars = set()
    result = ''
    while e < n:
        while e < n and input[e] not in chars:
            chars.add(input[e])
            e += 1        
        
        if len(result) < e - s:
            result = input[s:e]
        
        if e < n:
            while input[s] != input[e]:
                chars.remove(input[s])
                s += 1
            chars.remove(input[s])        
            s += 1
    return result

print(find_longest_substring_wo_repeating_char('aabbcde'))
print(find_longest_substring_wo_repeating_char('a'))
print(find_longest_substring_wo_repeating_char('ab'))
print(find_longest_substring_wo_repeating_char('abc'))
print(find_longest_substring_wo_repeating_char('abccc'))
print(find_longest_substring_wo_repeating_char('abcccdefg'))
# output:
# bcde
# a
# ab
# abc
# abc
# cdefg

- Chris July 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashSet;

public class UniqueSubString {

	public static void main(String[] args){
		System.out.println(findLongestSubString("aabbcde"));
		System.out.println(findLongestSubString("abccc"));
		System.out.println(findLongestSubString("abcccdefg"));		
	}

	public static String findLongestSubString(String str){
		HashSet<Character> set = new  HashSet<>();
		ArrayList<String> list = new ArrayList<>();
		int maxSizeIndex =0;
		int maxSize = 0;
		for(int i=0;i<str.length();i++){
			if(set.contains(str.charAt(i)) || i==str.length()-1) {
				
				
				String substr = "";				
				for(Character char2:set){
					substr+=char2;
				}
				if(i==str.length()-1) substr+=str.charAt(i);
				list.add(substr);
				if(maxSize < substr.length()) {
					maxSize = set.size();
					maxSizeIndex = list.size()-1;
				}					
				set.clear();
			}
			set.add(str.charAt(i));
		}
		return list.isEmpty()?null:list.get(maxSizeIndex);	
	}

}

- nvat July 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I dont see this code giving the right output for the following string:

abcdcefghi

- Nobel July 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This doesnt seem to be working for the following string:
abcdcefghi

- nobelxavier July 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void printLongestSubstringNoDuplicateChars(String s){
		
		int startIndex = 0, endIndex = 0;
		StringBuilder previousString = new StringBuilder();
		
		while(endIndex < s.length()){
			
			Set<Character> set = new HashSet<>();
			StringBuilder currentString = new StringBuilder();
			
			while(endIndex < s.length() && set.add(s.charAt(endIndex))){
				currentString.append(s.charAt(endIndex));
				endIndex++;
			}
			
			startIndex = endIndex;
			endIndex = startIndex;
			
			if(currentString.length() > previousString.length()){
				previousString = currentString;
			}
		}
		
		System.out.println(previousString.toString());
		
	}

- undefined July 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void printLongestSubstringNoDuplicateChars(String s){
		
		int startIndex = 0, endIndex = 0;
		StringBuilder previousString = new StringBuilder();
		
		while(endIndex < s.length()){
			
			Set<Character> set = new HashSet<>();
			StringBuilder currentString = new StringBuilder();
			
			while(endIndex < s.length() && set.add(s.charAt(endIndex))){
				currentString.append(s.charAt(endIndex));
				endIndex++;
			}
			
			startIndex = endIndex;
			endIndex = startIndex;
			
			if(currentString.length() > previousString.length()){
				previousString = currentString;
			}
		}
		
		System.out.println(previousString.toString());
		
	}

- Anonymous July 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;

public class LongestSubstringWithoutRepeatingCharacter {

	public static void main(String[] args) {
		
		String str = "abhdhknxkjankzj";
		String result = "";
		int index = 0;
		ArrayList<Character> list = new ArrayList<Character>();
		
		for(int i=0; index<str.length();){
			
			list.add(str.charAt(i));
			
			for(int j=i+1;j<str.length(); j++)
				
				if(list.contains(str.charAt(j))){
					
					index += list.indexOf(str.charAt(j));
					
					String subStr = (String) str.subSequence(i, j);
					
					if(subStr.length() > result.length())
						result = subStr;	
					
					break;
				}
				else
					list.add(str.charAt(j));
			
			list.clear();
			i = ++index;
		}
		
		System.out.println("Result = " + result);

	}

}

- Akash Deep July 07, 2017 | 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