Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?

Visit AONECODE.COM for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates of FB, LinkedIn, Amazon, Google & Uber etc.)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn, MS and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!

//Solution to question   O(n)
   boolean isPeriod(String s) {
        StringBuilder str = new StringBuilder(s + s);
        str.deleteCharAt(0);
        str.deleteCharAt(str.length() - 1);
        return strStr(str.toString(), s); //KMP strStr(T, S) to find if T has S in it.
    }

    //Solution to follow-up
    //This method looks for the repeating pattern in string
    private static String getPeriod(String string) { // O(n * n)
        //for every possible period size i, check if it's valid
        for (int i = 1; i <= string.length() / 2; i++) {
            if (string.length() % i == 0) {
                String period = string.substring(0, i);
                int j = i;
                while(j + i < string.length()) {
                    if (period.equals(string.substring(j, j + i))) {
                        j = j + i;
                        if(j == string.length()) { //period valid through entire string
                            return period;
                        }
                    } else {
                        break;
                    }
                }
            }

        }
        return null; //string is not periodic
    }

- aonecoding January 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main()
{
//unsigned char String[] = "xxxxxxxxx";
//unsigned char String[] = "abababababab";
unsigned char String[] = "aabbaaabba";
unsigned char *Pattern;
unsigned int Index = 0, pLength = 0, i, count = 1;

Pattern = &String[0];
pLength = 1;
for ( Index = 1; Index < sizeof(String)-1; )
{
for ( i = 0; i < pLength; i++ )
{
if ( Pattern[i] == String[Index])
Index++;
else
{
pLength++;
if ( pLength > (sizeof(String) / 2) )
pLength = sizeof(String);
Index = pLength;
count = 0;
break;
}
}
count++;
}

return 0;
}

- nikhildharashivkar January 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution in C++

#include <iostream>
#include <string>

bool isPeriodic(const std::string& str) {
  std::string test = str.substr(1) + str.substr(0, str.length() / 2);
  return test.find(str) != test.npos;
}

auto findPeriod(const std::string& str) {
  auto len = 0;
  std::string test = str.substr(1) + str.substr(0, str.length() / 2);
  while (test.find(str) != test.npos) {
    ++len;
    test.erase(test.length() - 1);
  }
  if (!len)
    return std::string();
  return str.substr(0, 1 + str.length() / 2 - len);
}

- S.M. January 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Alternative solution for findPeriod in C++

auto findPeriod(const std::string& str) {
  auto len = 1;
  std::string test = str.substr(1) + str[0];
  for (;  len <= str.length() / 2 && test.find(str) == test.npos; ++len) {
    test += str[len];
  }
  if (len > str.length() / 2)
    return std::string();
  return str.substr(0, len);
}

- S.M. January 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def periodic_string(s):
    for i in range(1,(len(s)/2) + 1):
        if len(s) % i == 0:
            period = s[0:i]
            step = j = i
            while j < len(s):
                if s[j:j+step] == period:
                    if j+step == len(s):
                        print period
                        return True
                    j += step
                else:
                    break
    return False

- sagarchaturvedi1 January 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def checkFrequency(s):
    pattern = ""
    i = 0
    while i < len(s):
        pattern += s[i]
        frequency = int(len(s)/len(pattern))
        if(pattern*frequency == s and frequency > 1):
            return True  
        i += 1
    
    return False

- nurci March 21, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def checkFrequency(s):
    pattern = ""
    i = 0
    while i < len(s):
        pattern += s[i]
        frequency = int(len(s)/len(pattern))
        if(pattern*frequency == s and frequency > 1):
            return True  
        i += 1
    
    return False

- nurciburci March 21, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String getPeriod(String str) {
        if (str == null || str.length() <= 1) return null;
        int len = str.length();
        int[] lps = new int[len];
        int prev = 0;
        for (int i = 1; i < len; i++) {
            char ch = str.charAt(i);
            char prevChar = str.charAt(prev);
            if (ch == prevChar) {
                prev++;
                lps[i] = prev;
            } else {
                if (prev == 0) {
                    lps[i] = 0;
                } else {
                    prev = lps[prev-1];
                    i--;
                }
            }
        }
        int lpsLength = lps[len-1];
        int leftLen = len - lpsLength;
        if (len % leftLen == 0) return str.substring(0, leftLen);
        return null;
    }

- Aim_Google March 30, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in Go

func isPeriodic(s string) (bool, string) {
	for i := 1; i <= len(s)/2; i++ {
		if len(s)%i != 0 {
			continue
		}
		p := s[:i]
		c := strings.Count(s, p)
		if i*c == len(s) {
			return true, p
		}
	}
	return false, ""
}

- Anonymous September 11, 2018 | 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