0% found this document useful (0 votes)
7 views13 pages

Google

Uploaded by

musirikechandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Google

Uploaded by

musirikechandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Google

4 hrs login time ,6 questions (overall)


Coding test: 2questions,60 min, hard
1)https://discuss.codechef.com/t/google-coding-problem-help/95356
(direct question )(given ans is correct in the link)

2)https://leetcode.com/discuss/interview-question/1934253/google-online-assessment-hybrid-se
quence (it contains the question (hybrid maximum)
(solution below)
3 ,4)
https://leetcode.com/discuss/interview-question/3760132/Google-SWE-Intern-OA-2023-(India)-
Both-Questions (it contains two questions)
3) code: (special subsequence)

#include<bits/stdc++.h>
using namespace std;
#define int long long
int MOD=1e9+7;
const int N = 100005;
int f[N],invf[N];

int power(int a,int b)


{
if(b==0)
return 1;
else
{
int x=power(a,b/2);
int y=(x*x)%MOD;
if(b%2)
y=(y*a)%MOD;
return y;
}
}
int add(int a,int b)
{
if(b==0)
return 0;
else
{
int x=add(a,b/2);
int y=(x+x)%MOD;
if(b%2)
y=(y+a)%MOD;
return y;
}
}
int inverse(int a)
{
return power(a,MOD-2);
}

int ncr(int n,int r)


{
if(n<0 || r<0 || n<r)
return 0;
int ans=f[n];
ans=(ans*invf[r])%MOD;
ans=(ans*invf[n-r])%MOD;
return ans;
}

void precomputeFactorials()
{
f[0]=1;
for(int i=1;i<N;i++)
f[i]=(f[i-1]*i)%MOD;
for(int i=0;i<N;i++)
invf[i]=inverse(f[i]);
}

int solve(int n, int k, string s) {


vector<int> freq(26,0);
for(int i=0;i<n;i++) {
freq[s[i]-'a']++;
}
sort(freq.begin(),freq.end(),greater<int>());
int cnt=0,cnt1=0;
for(int i=0;i<26;i++) {
if(freq[i]==freq[k-1]) {
cnt++;
if(i<k) cnt1++;
}
}

int ans = ncr(cnt,cnt1);


for(int i=0;i<k;i++) {
ans=(ans*freq[i])%MOD;
}
return ans;
}
int32_t main()
{
int t;
cin>>t;
precomputeFactorials();

while(t--)
{ int k;
cin>>k;

string s;
cin>>s;int n=s.size();
cout<< solve(n,k,s);

}
}
4) code : Find Arrays
#include<bits/stdc++.h>
using namespace std;
#define int long long
int mod = 1e9 +7;
const int LIMIT = 1001;
int digit[LIMIT];
int dp[101][LIMIT];
int get_sum(int n){
int sum=0;
while(n!=0){
sum+=(n%10);
n/=10;
}
return sum;
}
void pre_compute(){
for(int i=0;i<LIMIT;i++){
digit[i]=get_sum(i);
}
}
int no_of_ways(vector<int> &s,int n){
int ans=0;
dp[0][0]=1;
vector<int> pref(LIMIT,1);
for(int i=1;i<=n;i++){
for(int j=0;j<LIMIT;j++){
if(s[i-1]==digit[j]){
dp[i][j] = pref[j-1];
}
else {
dp[i][j]=0;
}
}
pref[0]=0;
for(int j=1;j<LIMIT;j++){
pref[j]=pref[j-1]+dp[i][j];
}
}
for(int i=0;i<LIMIT;i++){
ans+=dp[n][i];
ans%=mod;
}
return ans;
}

int32_t main(){
pre_compute();
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> s(n);
for(int i=0;i<n;i++){
cin>>s[i];
}
cout<<no_of_ways(s,n)<<"\n";

}
}

5)https://leetcode.com/discuss/interview-question/1366276/google-oa-prime-path
That contains code may work. (keepMotivated 494 July 27, 2021 11:46 PM (this one))

6)Median Path Question(below)


Code: (it works)
#include<bits/stdc++.h>
using namespace std;
vector<int>prime(1000001,0);
#define ll long long
void sieve()
{
prime[1] = 1;
for(int i = 2;i<=1000000;i++)
{
prime[i] = i;
}
for(int i = 2;i<=1000000;i++)
{
if(prime[i] == i && (long long)i*i <= 1000000)
{
for(int j = i*i; (long long)j <= 1000000 ; j+=i)
{
prime[j] = i;
}
}
}
}
int query(int n)
{
set<int>ans;
while(n != 1)
{
ans.insert(prime[n]);
n/=prime[n];
}
return ans.size();
}
int main()
{

sieve();
int t;
cin>>t;

while(t--)
{
int n;
cin>>n;
vector<vector<int>>G(n,vector<int>(n,0));
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
cin>>G[i][j];
}
}
vector<vector<int>>distinctprime(n,vector<int>(n));
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
{
distinctprime[i][j] = query(G[i][j]);
}
}
priority_queue<pair<ll,pair<ll,ll>>,vector<pair<ll,pair<ll,ll>>>,greater<pair<ll,pair<ll,ll>>> > pq;
vector<vector<ll>>dist(n,vector<ll>(n,LONG_MAX));
dist[0][0] = 0LL;
pq.push({0LL,{0,0}});
// cout<<dist[n-1][n-1];
while(!pq.empty())
{
auto it = pq.top();
pq.pop();
int x = it.second.first;
int y = it.second.second;
ll val = distinctprime[x][y];

for(int k = -val+x;k<=x+val;k++)
{
for(int j = -val+y;j<=y+val;j++)
{
if(k<0 || j<0 || k>=n || j>=n) continue;
if(k == x && j==y) continue;
if( abs(x-k) + abs(y-j) <= val)
{
if(dist[k][j] > dist[x][y] + sqrt(G[x][y])*1LL)
{
dist[k][j] = dist[x][y] + sqrt(G[x][y])*1LL;

pq.push({dist[k][j],{k,j}});
}
}
}
}
}

cout<<dist[n-1][n-1]<<"\n";
}
return 0;
}

You might also like