Google OA SWE Intern 2021 India
Anonymous User
1068

myself - A Final Year Student from Tier 3 college. I applied for Summer Internship role through Google's carrier site.

platform - HackerEarth
test pattern - 2 Coding Questions (1 hour)

Question 1 - N boxes are Arrranged in a line. Each box is colored either with white or black. You can select any number of boxes from the starting and put it behind the last box in the same sequence as it was placed initially. you can perform this operation atmost once.
For example string s="wbbb" . In one operation you can select "wb" and from the start and put it behind the last box "b".

Note: you cannot select all the n boxes.

You are required to find the minimum number of boxes whose colors must be reversed so that all the boxes in the sequence are of an alternate color, that is any two adjacent boxes are of different colors.

Input :  5
wwwwwbbww
wwwb
bwww
wwww
bbbbb

Output : 
3
1
1
2
2

my code

#include<bits/stdc++.h>
using namespace std;
 
const int N = 2e5 + 5;

string str;
void sol()
{
	cin>>str;
	string s;
	int m1=str.find("bw");
	int m2=str.find("wb");
	if(m1>m2)
	s=str.substr(m1,str.length())+str.substr(0,m1);
	else if(m2>m1)
	s=str.substr(m2,str.length())+str.substr(0,m2);
	else
	s=str;
	int a=0,b=0;
	for(int i=0;i<s.length();i+=2)
	{
		if(s[i]!='b')
		a++;
	}
	for(int i=1;i<s.length();i+=2)
	{
		if(s[i]!='w')
		a++;
	}
	for(int i=0;i<s.length();i+=2)
	{
		if(s[i]!='w')
		b++;
	}
	for(int i=1;i<s.length();i+=2)
	{
		if(s[i]!='b')
		b++;
	}
	cout<<min(a,b)<<endl;
}
int32_t main()
{
	IOS;
	int t;
	cin >> t;
        //t=1;
	while(t--)
	{
		sol();
	}
	return 0;
}

Correct me if i'm wrong..

Question 2 - You are given a tree of N nodes. The tree is rooted at node 1.Each tree node has a value associated with it and it is represented as value(i). for each node you are required to determine the closest ancestor that contains values that are co-primes to the current node value. If no such nodes exists then print -1.

Input format
first line T no.of Test cases.
first line of each test case contains an integer N denoting the number of nodes in the tree.
Next line contains N integers where i-th integer denotes value at node i (value(i)).
Next N-1 lines contains 2 space seperated integers u and v denoting an edge between u and v.

Input : 
2
8
16 17 12 7 20 18 7 8
6 8
3 8
8 1 
2 7
2 3 
7 5 
2 4
6
5 19 12 10 9 16
4 3
5 3
6 2
6 1
4 6

Output :
-1 3 -1 2 7 -1 2 -1
-1 6 1 -1 4 1

However I failed to answer this..!

Comments (5)