Ivycomptech Interview Question


Country: India
Interview Type: In-Person




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

// ZoomBA
exists ( [1: #|s|/2 ] ) :: {
   pat = '(' + s[0:$.o] +')+' // create pattern 
   s =~ pat // s matches pattern?
}

- NoOne October 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Start with subString len=ceil(string length/2) to 1
if len is a multiple of len then check if consecutive substrings of size 'len' are same.

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

def isPattern(input_string) :
    pattern_length = 1
    while (pattern_length <= len(input_string)):
        if(len(input_string) % pattern_length == 0) : # if pattern_length divides input_string check
                                                      # else increase pattern by one character

            pattern = input_string[0:pattern_length]   # create pattern as substring of input_string
            placeholder = pattern_length
            while( pattern == input_string[placeholder : pattern_length + placeholder]) :
                if placeholder == int(len(input_string) - pattern_length):
                    return {True : pattern}
                else :
                    placeholder = placeholder + pattern_length
        pattern_length = pattern_length + 1
    return False

# Test
print(isPattern('abcabcabc')) # {True: 'abc'}
print(isPattern('abcdabababab')) # False

- Luke Rhinehart July 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am interpreting this problem as: "Given a string s, check whether it is made of one or more repetitions of the same substring"

Here is a simple solution in java.

public static boolean hasRepeated(String input) {
        assert(input.length() > 0);
        
        char start = input.charAt(0);
        for (int i = 1; i < input.length(); i++) {
            char current = input.charAt(i);
            if (current != start) {
                continue;
            }
            if (matchesRepeated(input.substring(0, i), input.substring(i, input.length()))) {
                return true;
            }
        }
        
        return false;
    }
    
    public static boolean matchesRepeated(String pattern, String input) {
        if (input.length() % pattern.length() != 0) {
            return false;
        }
        
        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) != pattern.charAt(i % pattern.length())) {
                return false;
            }
        }
        
        return true;
    }

- bocajuniors July 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For c++ code...complexity O(n)

bool repeatedSubstr(std::string data, std::string& pattern){
  std::vector<std::string> subs;
  std::string temp = ""; 
  for (auto each : data){
    if (each == data[0]){
      subs.push_back(data[0] + temp);
      temp = "";
    }
    else
      temp += each;
  }
  subs.push_back(data[0] + temp);
  if (subs.size() <= 2){
    pattern = "";
    return false;
  }else{
    pattern = subs[1];
    for (int i = 2; i < (int)subs.size(); i++)
      if (subs[i] != pattern){
        pattern = "";
        return false;
      }
    return true;
  }
}

For python code...complexity O(n)

def repeatedSubstring(data):
	subs = data.split(data[0])[1::]
	pattern = subs[0]
	if len(subs) == 1:
		return False
	for each in subs:
		if each != pattern:
			return False
	return data[0] + pattern

Update even better solution without vector in c++...complexity O(n)

bool repeatedSubstring(std::string data, std::string& pattern){
  std::string temp = "";
  pattern = "";
  for(int i = 1; i < (int)data.size(); i++){
   if (data[i] != data[0]){
     temp += data[i];
   }else{
     if (pattern == "")
        pattern = data[0] + temp;
     else if (pattern != (data[0] + temp)){
        pattern = "";
        return false;
     }
     temp = "";
    }
  }
  temp = data[0] + temp;
  if (temp != pattern)
    pattern = "";
   return (pattern == temp);
}

Update:: Even better answer without vector

bool repeatedSubstring(std::string data, std::string& pattern){
  std::string temp = "";
  pattern = "";
  for(int i = 1; i < (int)data.size(); i++){
   if (data[i] != data[0]){
     temp += data[i];
   }else{
     if (pattern == "")
        pattern = data[0] + temp;
     else if (pattern != (data[0] + temp)){
        pattern = "";
        return false;
     }
     temp = "";
    }
  }
  temp = data[0] + temp;
  if (temp != pattern)
    pattern = "";
   return (pattern == temp);
}

- mayukh July 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool repeatedSubstr(std::string data, std::string& pattern){
std::vector<std::string> subs;
std::string temp = "";
for (auto each : data){
if (each == data[0]){
subs.push_back(data[0] + temp);
temp = "";
}
else
temp += each;
}
subs.push_back(data[0] + temp);
if (subs.size() <= 2){
pattern = "";
return false;
}else{
pattern = subs[1];
for (int i = 2; i < (int)subs.size(); i++)
if (subs[i] != pattern){
pattern = "";
return false;
}
return true;
}
}

- rimmi deshlehra July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{bool repeatedSubstr(std::string data, std::string& pattern){
std::vector<std::string> subs;
std::string temp = "";
for (auto each : data){
if (each == data[0]){
subs.push_back(data[0] + temp);
temp = "";
}
else
temp += each;
}
subs.push_back(data[0] + temp);
if (subs.size() <= 2){
pattern = "";
return false;
}else{
pattern = subs[1];
for (int i = 2; i < (int)subs.size(); i++)
if (subs[i] != pattern){
pattern = "";
return false;
}
return true;
}
}

- rimmi deshlehra July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isRepeatedString(String source, String find){
int count = 0;
for(int i=0; i<source.length(); i++){
String s = source.substring(i,find.length());
if(s.equalsIgnoreCase(find)){
count++;
}
}
if(count > 1){
return true;
}
else return false;
}

- Vighneswar Rao Bojja July 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isRepeatedString(String source, String find){
int count = 0;
for(int i=0; i<source.length(); i++){
  String s = source.substring(i,find.length());
  if(s.equalsIgnoreCase(find)){
   count++;
  }
}
  if(count > 1){
   return true;
 }
 else return false;
}

- Vighneswar Rao Bojja July 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isRepeatedString(String source, String find){
int count = 0;
for(int i=0; i<source.length(); i++){
  String s = source.substring(i,find.length());
  if(s.equalsIgnoreCase(find)){
   count++;
  }
}
  if(count > 1){
   return true;
 }
 else return false;
 }

- vighneswarraobojja July 29, 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