Google Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

static int[] maxLCS(int[] a, int[] b) {
        int[][] op = new int[a.length + 1][b.length + 1];
        for(int i = 1; i < a.length + 1; i++) {
            for(int j = 1 ;j < b.length + 1; j++) {
                if(a[i-1] == b[j-1]){
                    op[i][j] = op[i-1][j-1] + 1;
                }else {
                    op[i][j] = Math.max(op[i-1][j], op[i][j-1]);
                }
            }
        }
        int maxLen = op[a.length][b.length];
        int[] result = new int[maxLen];
        int ap = a.length;
        int bp = b.length;
        while(ap > 0 && bp > 0) {
            if(a[ap-1] == b[bp-1]) {
                result[maxLen-1] = a[ap-1];
                maxLen--;
                ap--;
                bp--;
            }else if(op[ap-1][bp] <= op[ap][bp-1]) {
                bp--;
            }else {
                ap--;
            }
        }
        return result;
    }

Looking for interview experience sharing and mentors?
Visit A++ Coding Bootcamp at aonecode.com.

Taught by experienced engineers/interviewers from FB, Google and Uber,
our ONE TO ONE courses cover everything in an interview including
latest interview questions sorted by companies,
SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)
ALGORITHMS (conquer DP, Graph, Greed and other advanced algo problems),
and mock interviews.

Our students got offers from Google, Uber, Facebook, Amazon and other top companies after a few weeks of training.

Welcome to email us with any questions. Thanks for reading.

- acoding167 May 12, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void LongestSubSequence(int arr[], int arr2[],int n1,int n2)
{
	map<int, int> mapMerged = { make_pair(0,0) };
	map<int, int>::iterator mapIt;

	int largest = (n1 > n2) ? n1 : n2;
	for (int i = 0; i < largest; i++)
	{
		if (i < n1)
		{
			mapMerged[arr[i]]++;
		}
		if (i < n2)
		{
			mapMerged[arr2[i]]++;
		}
	}
	
	//Print sequence now.
	for (mapIt = mapMerged.begin(); mapIt != mapMerged.end(); mapIt++)
	{
		if(mapIt->second > 1)
			printf("%d ",mapIt->first);
	}
}

int main()
{
	int arr1[] = {1,5,2,6,3,7,9,10,55,34};
	int arr2[] = { 5,6,7,1,2,3,10 };

	int n1 = sizeof(arr1) / sizeof(int);

	int n2 = sizeof(arr2) / sizeof(int);



	LongestSubSequence(arr1,arr2,n1,n2);
}

- rsullad May 14, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C#

Cost: O(m + n)

private static List<int> lcs(int[] array1, int[] array2)
        {
            List<int> r1 = lcsInternal(array1, array2);
            List<int> r2 = lcsInternal(array2, array1);
            List<int> result;

            // Get the longest
            if (r1.Count >= r2.Count) { result = r1; }
            else { result = r2; }

            return result;
        }

        private static List<int> lcsInternal(int[] array1, int[] array2)
        {
            int l1 = array1.Length;
            int l2 = array2.Length;

            List<int> resultList = new List<int>();

            // Initially start with 0
            int startj = 0;

            bool done = false;

            for (int i = 0; i < l1 && !done; i++)
            {
                int v1 = array1[i];

                for (int j = startj; j < l2; j++)
                {
                    int v2 = array2[j];
                    
                    // Not the same value, go to the next j
                    if (v1 != v2) { continue; }

                    // v1 == v2
                    // Store the result
                    resultList.Add(v1);

                    // Store the current j + 1 because it'll start there on
                    // the next iteration
                    startj = j + 1;
                    if (startj >= l2)
                    {
                        done = true;
                    }

                    // Stop the current j, and continue with i
                    break;
                }
            }

            return resultList;
        }

- therearetwouws May 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Try this lcs(new[]{1,2,3,4,5,6}, new[]{6,2,3,4,5,1}) to check your code

- Alex June 05, 2019 | Flag


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