Google Interview Question for Java Developers


Country: United States




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

public boolean find(int[][] a, int[][] b) {
        int start = b[0][0];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {
                if (a[i][j] == start && a.length - i >= b.length && a.length - j >= b.length) {
                    boolean res = matchArray(a, b, i, j);
                    if (res) return res;
                }
            }
        }
        return false;
    }

    public boolean matchArray(int[][] a, int[][] b, int i, int j) {
        int jOrig = j;
        for (int i1 = 0; i1 < b.length; i1++) {
            for (int j1 = 0; j1 < b.length; j1++) {
                if (b[i1][j1] != a[i][j++]) {
                    return false;
                }
            }
            i++;
            j = jOrig;
        }
        return true;
    }

- Anonymous February 12, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
//Author = Jean

bool isSubMatrix(const std::vector<std::vector<int>>& small, const std::vector<std::vector<int>>& large){
	int sizeYLarge = large.size();
	int sizeXLarge = large[0].size();
	int sizeYSmall = small.size();
	int sizeXSmall = small[0].size();
	int li,lj,si,sj;
	for (li = 0 ; li < sizeXLarge ; li++){
		for (lj = 0 ; lj < sizeYLarge ; lj++){
			if (large[li][lj] == small[0][0]){
				for (si = 0 ; si< sizeXSmall ; si ++){
					for (sj = 0 ; sj < sizeYSmall ; sj++) {
						std::cout<<"large "<<large[si+li][sj+lj]<<"small "<<small[si][li]<<std::endl;
						if (large[si+li][sj+lj]!=small[si][sj]){
							break;
						}
					}
				}
			}
		}
	}
	if (si == sizeXSmall && sj == sizeYSmall)
			return true;
	return false;
	}

main (){
	std::vector<std::vector<int>> large={{1 ,2 ,  3},
										 {4 ,5 ,  6},
										 {7 ,8 ,  9},
										 {10,11, 12}};

	std::vector<std::vector<int>> small={{5, 6},
										 {8,10}};
	std::cout<<isSubMatrix(small,large);

}

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

#include <iostream>
#include <vector>
//Author = Jean

bool isSubMatrix(const std::vector<std::vector<int>>& small, const std::vector<std::vector<int>>& large){
	int sizeYLarge = large.size();
	int sizeXLarge = large[0].size();
	int sizeYSmall = small.size();
	int sizeXSmall = small[0].size();
	int li,lj,si,sj;
	for (li = 0 ; li < sizeXLarge ; li++){
		for (lj = 0 ; lj < sizeYLarge ; lj++){
			if (large[li][lj] == small[0][0]){
				for (si = 0 ; si< sizeXSmall ; si ++){
					for (sj = 0 ; sj < sizeYSmall ; sj++) {
						std::cout<<"large "<<large[si+li][sj+lj]<<"small "<<small[si][li]<<std::endl;
						if (large[si+li][sj+lj]!=small[si][sj]){
							break;
						}
					}
				}
			}
		}
	}
	if (si == sizeXSmall && sj == sizeYSmall)
			return true;
	return false;
	}

main (){
	std::vector<std::vector<int>> large={{1 ,2 ,  3},
										 {4 ,5 ,  6},
										 {7 ,8 ,  9},
										 {10,11, 12}};

	std::vector<std::vector<int>> small={{5, 6},
										 {8,10}};
	std::cout<<isSubMatrix(small,large);

}

- Jean March 17, 2018 | 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