Facebook Interview Question for Software Engineers

• 0

Country: United States
Interview Type: In-Person

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

``````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
}``````

Looking for interview experience sharing and mentors?
Visit A++ Coding Bootcamp at aonecode.com.

Taught by experienced engineers/interviewers from FB, Google and Uber,
our ONE TO ONE courses cover everything in an interview including
latest interview questions sorted by companies,
SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)
ALGORITHMS (conquer DP, Graph, Greed and other advanced algo problems),
and mock interviews.

Our students got offers from Google, Uber, Facebook, Amazon and other top companies after a few weeks of training.

Welcome to email us with any questions. Thanks for reading.

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

``````def factors(n):
f=[]
for i in range(1,n):
if n%i==0:
f.append(i)
return f

def periodic(s):

f=factors(len(s))
for i in f:
m=len(s)/i
#return s[:i]*3
if s[:i]*int(m)==s:
return True
return False

print(periodic('abcabc'))
def factors(n):
f=[]
for i in range(1,n):
if n%i==0:
f.append(i)
return f

def periodic(s):

f=factors(len(s))
for i in f:
m=len(s)/i
#return s[:i]*3
if s[:i]*int(m)==s:
return True
return False``````

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

``````def factors(n):
f=[]
for i in range(1,n):
if n%i==0:
f.append(i)
return f

def periodic(s):

f=factors(len(s))
for i in f:
m=len(s)/i
#return s[:i]*3
if s[:i]*int(m)==s:
return True
return False``````

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

What am I missing about example number 2 in the question:

S = "xxxxxx", n = 1, then P = "x"

This doesn't seem consistent with the other examples.
In the first example, if n=3 and P=ab, then 3ab => ababab, that makes sense.
In the third example, if n = 2 and P=aabba, then 2aabba => aabbaaabba, that make sense.

But for exmple 2, if n=1 and P=x, then 1x should be x, not xxxxxx. I feel that in this case, if the P is x, then n should be 6, because 6x => xxxxxx

What am I missing? Am I fundamentally misunderstanding the problem here?

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

a slightly improved version

``````#!/usr/bin/env python3
"""
Find whether string S is periodic. Periodic indicates S = nP. e.g.
if S = abcabc, then n = 3, and P = abc
if S = xxxx, n = 1, and P = x
follow up, given string S, find out repetitive pattern of P
"""
def factors(n):
f = []
for i in range(1, n):
if n % i == 0:
f.append(i)
return f
def periodic(s):
f = factors(len(s))
for i in f:
m = len(s)/i
if s[:i] * int(m) == s:
print(i, s[:i], s)
return True
return False
# driver
if __name__ == "__main__":
assert True == periodic('abcabc')
assert False == periodic('abcabcd')``````

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.

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.