## Facebook Interview Question for SDE1s

Country: United States

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

Solution in python in case of a invalid ip address the function returns -1

``````import re

ip_regex = r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\$'

def compute_ip(s):
ip_digits = re.match(ip_regex, s)

if not ip_digits:
return -1

digits = map(int, ip_digits.groups())
for x in digits:
if x <= 0 or x > 255:
return -1

ip_value = 0
for pos, x in enumerate(digits):
if pos == 0:
ip_value += x << 24
elif pos == 1:
ip_value += x << 16
elif pos == 2:
ip_value += x << 8
else:
ip_value += x

return ip_value``````

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

I think it should be done as

1x256^3 + 2x256^2 + 3x256^1 + 4x256^0

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

``````function IPtoInt(string \$IP): int {

\$Bytes = array_map('intval', explode('.', \$IP));

\$IP = '';
foreach (\$Bytes as \$Byte) {
if (\$Byte < 0 || \$Byte > 255) {
return -1;
}

\$IP .= chr(\$Byte);
}

return hexdec(bin2hex(\$IP));
}``````

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.