## Google Interview Question for Software Engineers

• 3

Country: United States

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

Looking for help on interview preparation?
Visit AONECODE.COM for ONE-TO-ONE private lessons by FB, Google and Uber engineers!

Customized course covers
System Design (for candidates of FB, LinkedIn, AMZ, Google and Uber etc)
Algorithms (DP, Greedy, Graph etc. every aspect you need in a coding interview and Clean Coding)
Interview questions sorted by companies
Mock Interviews

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

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

``````boolean hasFactorKSubArray(int[] arr, int k) {
for(int i = 1; i < arr.length; i++) {
arr[i] += arr[i - 1]; //becomes a prefix sum array of arr (can be recovered if necessary)
}
for(int hi = arr.length - 1; hi >= 1; hi-- ) {
for(int lo = 0; lo <= hi - 1; lo++) {
if((arr[hi] - lo == 0 ? 0: arr[lo - 1]) % k == 0) {
return true;
}
}
}
return false;
}``````

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

package findSubArray;
import java.util.*;

public class findSubArray {

public static int findsubArray(int arr[], int k) {
int n = arr.length, i, j;
int array[][] = new int[n][n];
for(i=0;i<n;i++) {
array[i] = arr[i];
}
for(i=0;i<n;i++) {
array[i] = arr;
}
for(i=1; i<n; i++) {
for(j=i;j<n;j++) {
array[i][j] = arr[j] * array[i-1][j-1];
System.out.println("i: "+i+" j: "+j+" arr[i-1][j-1]: "+array[i-1][j-1]+" arr[i][j]: "+array[i][j]);
if(array[i][j] % k ==0) {
if(i != 0) {
System.out.println("End of subarray is: "+arr[j]);
return i+1;
}
}
}
}
return 0;

}
public static void main(String[] args) {
// TODO Auto-generated method stub
int array[] = {1, 9, 16, 5, 4, 3, 2};
int k = 720;
int answer = findsubArray(array, k);
System.out.println("Minimum lenth of subarray is: "+answer);
}

}

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

If those the number of those lamps fits into an integer of some sort (or really, this requirement could be just the ranges you flip), I think this solution could work too (not tested):

``````int get_range(unsigned long n, int i, int j) {
return (n >> i) & ((1 << j) - 1);
}

int flip(unsigned long n, int i, int j) {
return n + (~get_range(n, i, j) - get_range(n, i, j));
}

int isOn(unsigned long n, int i) {
return n & (1 << i);
}``````

Space O(N), Update O(1) if the bitwise operators are O(1) as expected, query O(1)

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

``````hasSumMultK :: (Map.Map Int Int) -> Int ->[Int] -> Int-> Bool
hasSumMultK m s [] _ = Map.member s m

hasSumMultK m sum (x:xs) k  = let newSum = (sum + x) `mod` k
in (Map.member sum m) ||
hasSumMultK (Map.insert sum sum m) newSum xs k

hasSumMultOfK :: [Int] -> Int -> Bool
hasSumMultOfK = hasSumMultK Map.empty 0``````

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.