Amazon Interview Question for SDE-3s


Country: United States
Interview Type: In-Person




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

static String findLargestRepeatedSubString(String s){
		if(s == null)
			throw new IllegalArgumentException("Provided String is null");
		if(s.length() == 1){
			return null;
		}
		String result = null;
		for(int start = 0; start < s.length(); start++){
			for(int end = start+1; end <= s.length(); end++) {
				String subString = s.substring(start, end);
				if(s.substring(end).contains(subString)){
					if(result == null)
						result = s.substring(start, end);
					else if(subString.length() > result.length()){
						result = subString;
					}
				}
			}
		}
		return result;
	}

- Cody March 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static String findLargestRepeatedSubString(String s){
		if(s == null)
			throw new IllegalArgumentException("Provided String is null");
		if(s.length() == 1){
			return null;
		}
		String result = null;
		for(int start = 0; start < s.length(); start++){
			for(int end = start+1; end <= s.length(); end++) {
				String subString = s.substring(start, end);
				if(s.substring(end).contains(subString)){
					if(result == null)
						result = s.substring(start, end);
					else if(subString.length() > result.length()){
						result = subString;
					}
				}
			}
		}
		return result;
	}

- Cody March 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string FindLongestRepSubstring(string s)
{
        if(s == null)
	   return throw new ArgumentException("s");
	HashTable<string, int> h = new HashTable<string, h>();
	string longRepStr = null;	
	for(int i=0;i<s.Length; i++)
	{
		for(int = i+1; j<s.Length; j++)
			{
			    string nextSubStr = s.substring(i, j);
			    if(h.ContainsKey(nextSubStr))
				{
				    h[nextSubStr].Value++;
				    if(longRepStr == null)
				    {
					longRepStr = nextSubStr;
				    }
				    else if(longRepStr.Length < nextSubStr.Length) //if(h[longRepStr].Value < h[nextSubStr].Value)
				    {					
					longRepStr = nextSubStr;
				    }
				}
			    else
				{
				   h[nextSubStr].Value = 1;
				}
			   }
       }
       return longRepStr;
}

- fa1987 March 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Above codes time Complexity is n^3. We can do this n^2 using dynamic programming.

- hrshchaitanya April 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
// Time - O(n^2), Space - O(n^2)

static String findLongestRepeatedSubString(String s) {
if(s==null || s.trim().length()==0)
return "";
int len = s.length();
int[][] matrix = new int[len][len];
int maxLen = 0;
int maxIndex = 0;
for(int i=0; i< len; i++) {
for(int j=i+1; j < len; j++) {
if(s.charAt(i)==s.charAt(j)) {
matrix[i][j] = ((i-1<0 || j-1<0)?0:matrix[i-1][j-1]) +1;
if(matrix[i][j]>maxLen) {
maxLen = matrix[i][j];
maxIndex = j;
}
}
}
}

return s.substring(maxIndex-maxLen+1,maxIndex+1);
}
}

- hrshchaitanya April 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use a suffix tree. Find the deepest node having more than one child.

- lalat.nayak July 06, 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