## F5 Networks Interview Question for Software Engineer / Developers

• 0

Country: United States
Interview Type: Phone Interview

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

``````public int numberOfFactors(int n) {
int totalFactors = 0;
int i = 1;
while (i <= n) {
if (n % i == 0) {
totalFactors++;
}
i++;
}
}``````

Is the total number of factors of N to be returned, or return the actual factors as well?

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

``````-- Filter all factors of n
factorsOfN n = length \$ filter ((==0). mod n) [1..n]

-- filter all factors of n from 1..sqrt of n - 1 for squares
factorsOfN' n = (cSqrt - trSq) - 1 + 2*length (filter ((==0) . mod n) [2.. trSq])
where
trSq = truncate \$ sqrt \$ fI n
cSqrt = ceiling \$ sqrt \$ fI n``````

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

O(sqrt(n)) algorithm:

``````factors = 0
for i in range(int(ceil(sqrt(n)))):
if n % i == 0 and sqrt(n) != i:
factors += 2
elif n % i == 0:
factors += 1``````

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

Based on Anonymous's O(sqrt(n)) algorithm. Not entirely sure about the ceil(sqrt(n) + 0.0001) part though.

``````from math import *

def num_factors(n):
factors = 2
for i in range(2, int(ceil(sqrt(n) + 0.0001))):
if n % i == 0:
factors += 1 if i*i == n else 2
return factors

print num_factors(12)``````

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

0.0001 is added to ensure sqrt(n) is included in the iteration. The for..range in Python is exclusive at the closing bound.

This is the most efficient solution btw.

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

public int numberOfFactors(int n) {
int totalNumOfFacts = 0;
for(int i=1;i<=n/2;i++) {
if(n%i == 0)
totalNumOfFacts++;
}
return ++totalNumOfFacts;
}

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

This solution has the best case scenario complexity of O(log(sqrt(n)). The worst case scenario takes O(sqrt(n)) time. The idea is to find prime factors and their numbers. Then we just need to calculate the numbers each prime factor occurs in the input number.

``````private int FactorsNumber(int n)
{
var m = n;
var res = 1;
for (int d = 2; d <= Math.Sqrt(m); d++)
{
var k = 1;
while (m % d == 0)
{
m /= d;
k++;
}
if (k > 1) res *= k;
}
if (m > 1) res *= 2;
return res - 1;
}``````

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.