Google Interview Question for Software Engineers


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

- aonecoding January 13, 2018 | Flag Reply
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[0][i] = arr[i];
}
for(i=0;i<n;i++) {
array[i][0] = arr[0];
}
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);
}

}

- Varsha Jayaraman January 13, 2018 | Flag Reply
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)

- abnidg March 20, 2018 | Flag Reply
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

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

Naive solution with Time Complexity O(n**2), Auxiliary Space O(1). Can't figure out a linear solution

def hasSubSum(arr: [int], k: int) -> bool:
	for i in range(len(arr) - 1):
		sum = arr[i]
		for j in range(i + 1, len(arr)):
			sum += arr[j]
			if sum % k == 0:
				return True
	return False

- nicolarusso March 12, 2020 | 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