Zoho Coding Questions and Solutions

Zoho Coding Questions

Coding is one of the most important sections in Zoho. Here are some questions, based on the pattern and difficulty level of the Previous Year’s Zoho Coding Questions.

Zoho coding questions

Question 1

Your task is to complete a function “count_heads()” that takes two inputs N and R. The function should return the probability of getting exactly R heads on N successive tosses of a fair coin. A fair coin has an equal probability of landing a head or a tail (i.e. 0.5) on each toss.

Example 1

  • Input: 1 1
  • Output: 0.500000

Example 2

  • Input: 4 3
  • Output: 0.250000
Run
#include<bits/stdc++.h>
using namespace std;
 
double comb(int n,int r)
{
    if(r==0) return 1;
    return n*comb(n-1,r-1)/r;
}
int main()
{
   int n,r;
   cin>>n>>r;
   cout<<comb(n,r)/(1<<n);
}
Run
import java.util.*;
class Main
{
public static int fact(int n)
{
  if(n==0)
   return 1;
  return n*fact(n-1);  
 }
public static double count_heads(int n, int r)
{
    double res;
    res = fact(n) / (fact(r) * fact(n - r));
    res = res / (Math.pow(2, n));
    return res;
}

public static void main(String[] args)
{
   Scanner sc=new Scanner(System.in);
   int n=sc.nextInt();
   int r=sc.nextInt();
   System.out.println(count_heads(n,r));
}
}
Run
def comb(n, r):
    if r==0:
        return 1
    return n*comb(n-1, r-1)/r
    
n, r = map(int, input().split())
print(comb(n,r)/(1<<n))

Question 2

Write a program that will print the sum of diagonal elements of a 10X10 matrix. The program will take a total of 100 numbers as input (10 numbers will be input per line and each number will be separated by a space).

Example 1

  • Input:    1  2 3 4 5 6 7 8 9 0 
                   0 1 2 3 4 5 6 7 8 0
                   3 4 5 6 7 8 9 6 4 0
                   2 3 4 5 6 7 8 9 3 2
                   3 4 5 6 7 4 3 2 1 3
                   3 4 5 6 2 4 4 2 4 6
                   2 3 4 6 2 4 6 2 3 5
                   2 3 5 6 2 4 6 2 3 5
                   2 4 6 2 1 4 3 3 5 2
                   3 3 5 2 4 6 2 1 4 6
  • Output:  42

Example 2

  • Input:   1 22 33 44 55 66 77 88 99 100
                  100 1 88 77 66 55 44 33 22 11
                  88 88 1 66 55 44 33 22 11 100
                  88 77 66 1 44 33 22 11 100 99
                  77 66 55 44  1 22  11 88 99 100
                  66 55 44 33 22 1 77 88 99 100
                  44 33 22 11 100 99 1 77 66 55
                  33 22 11 100 99 88 77 1 55 44
                  22 11 100 99 88 77 66 55 1 33
                  100 11 22 33 44 55 99 88 77 1
  • Output: 10

Run

#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<vector<int>> v(10,vector<int>(10));
    for(int i=0;i<10;i++)
    for(int j=0;j<10;j++) cin>>v[i][j];
    int sum=0;
 
    for(int i=0;i<10;i++)
    sum+=v[i][i];
    cout<<sum;
}
Run
import java.util.*;
public class Main
{
  public static void main(String[] args)
{
   Scanner sc=new Scanner(System.in);
   int arr[][]=new int[10][10];
  for(int i=0;i<10;i++)
 {
    for(int j=0;j<10;j++)
       arr[i][j]=sc.nextInt();
}

int sum=0;
 for(int i=0;i<10;i++)
 sum=sum+arr[i][i];
 System.out.println(sum);
}
}
Run
L=[]
sum=0
for i in range(10):
    l=list(map(int,input().split()))
    L.append(l)
    sum+=L[i][i]
print(sum)

Question 3

Write a program that will take one string as input. The program will then remove vowels a, e, i, o, and u (in lower or upper case ) from the string. If there are two or more vowels that occur together then the program shall ignore all of those vowels.

Example 1

  • Input:  Cat
  • Output:  Ct

Example 2

  • Input:  Compuuter
  • Output: Cmpuutr
Run
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    string s,s1="";
    getline(cin,s);
    map<char,int> m;
    m['a']=1;m['e']=1;m['i']=1;m['o']=1;m['u']=1;
    m['a'-32]=1;m['e'-32]=1;m['i'-32]=1;m['o'-32]=1;m['u'-32]=1;
    if(s.length()==0) return 0;
    if(s.length()==1) {if(m[s[0]]==0) cout<<s; return 0;}
    else if(m[s[0]]==1&&m[s[1]]==0) {}
    else s1+=s[0];
    for(int i=1;i<s.length();i++)
    {
        if(m[s[i-1]]==0 && m[s[i]]==1 && m[s[i+1]]==0) continue;
        s1+=s[i];
    }    
    cout<<s1;
}
Run
import java.util.*;
public class Main
{
 public static boolean isVowel(char ch)
{
  return ch=='a' || ch=='e' || ch=='o' || ch=='i' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U';
}
  public static void main(String[] args)
{
  Scanner sc=new Scanner(System.in);
  String str=sc.next();
  char arr[]=str.toCharArray(); 
 String res="";
 if(!isVowel(arr[0]))
  res+=arr[0];
 for(int i=1;i<arr.length;i++)
 {
    if(isVowel(arr[i])&&isVowel(arr[i-1]))
      res+=arr[i-1]+""+arr[i];
     if(!isVowel(arr[i]))
     res+=arr[i];
 }
System.out.println(res);
}
}
Run
l=['a','e','i','o','u','A','E','I','O','U']
s=input()
s+=" "
s1=""
if len(s)==0:
    print("")
elif len(s)==1:
    if s[0] not in l:
        print(s)
else:
    if s[0] in l and s[1] in l:
        s1+=s[0]
    elif s[0] not in l :
        s1+=s[0]
 
    for i in range(1,len(s)-1):
        if s[i-1] not in l and s[i] in l and s[i+1] not in l:
            continue
        s1+=s[i]
 
    print(s1)

Question 4

Write a program that will take a string as input. The program will then determine whether each left parenthesis ‘(’ has a matching right parenthesis ‘)’ and also all the ‘)’ has a  consecutive ‘(‘. If so, the program will print 0 else the program will print 1.

Example 1

  • Input: HELLO AND (WELCOME (TO THE) TCEA (CONTEST)TODAY)IS (SATURDAY())
  • Output: 0

Example 2

  • Input: (9*(7-2)*(1*5)
  • Output: 0
Run
#include<bits/stdc++.h>
using namespace std;int main()
{
    string s;
    int c=0;
    getline(cin,s);
    for (auto i:s)
    {
        if(i=='(') c++;
        if(i==')') c--;
    }
    cout<<(c==0);
}
Run
s=input()
c=0
for i in s:
    if i =='(':
        c+=1
    elif (i==')') and c>0 :
        c-=1
print(int(c>0))

Question 5

Write a program to find out and display prime numbers from the given list of integers. The program will accept input in two lines. First-line contains a number indicating the total number of integers in the list and the second line contains integers separated by spaces.

Example 1

  • Input: 5
               4 6 9 3 7
  • Output:  3 7

Example 2

  • Input:  10
                 8 10 3 12 7 15 11 2 17 26
  • Output:  3 7 11 2 17
Run
#include<bits/stdc++.h>
using namespace std;
map<int,int> m;
bool ifPrime(int a)
{
    if(m[a]==2||m[a]==1) return m[a]-1;
    if((a&1)==0) {m[a]=1;return false;}
    for(int i=3;i<=sqrt(a);i+=2)
    if(a%i==0) {m[a]=1;return 0;}
    m[a]=2;
    return 1;
}
int main()
{
    m[2]=2;
    int n,a;cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        if(ifPrime(a)) cout<<a<<" ";
    }
}
Run
import java.util.*;
public class Main
{
 public static boolean isPrime(int n)
{
  if(n<=1)
  return false;
 if(n==2 || n==3)
 return true;
 if(n%2==0 || n%3==0)
  return false;
 for(int i=5;i<=Math.sqrt(n);i=i+6)
{
    if(n%i==0 || n%(i+2)==0)
      return false;
}
return true;
}
  public static void main(String[] args)
{
   Scanner sc=new Scanner(System.in);
   int n=sc.nextInt();
   int arr[]=new int[n];
  for(int i=0;i<n;i++)
    arr[i]=sc.nextInt();
  ArrayList prime=new ArrayList<>();
  for(int i=0;i<n;i++)
   if(isPrime(arr[i]))
    prime.add(arr[i]);
  Iterator itr=prime.iterator();
  while(itr.hasNext())
  {
    System.out.print(itr.next()+" ");
 }
}
}

Question 6

Write a program that will take a number as input. The program will convert the inputted number to a format, which will contain an integer part and a fraction part. The fraction part needs to be reduced to its lowest representation. The input will not contain any repeating decimals e.g. 1.3333…33. The output will be :

The integer part of the number ++fraction using a ’/’

Example 1 

  • Input:  2.95
  • Output:  2 19/20

Example 2

  • Input:  3.08
  • Output:  3 2/25
Run
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    string s;
    cin>>s;
    string s1;
    int idx=-1;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='.') idx=i;
        else s1+=s[i];
    }
    int a=stoi(s1);
    int p=1;
    if(idx!=-1)
    for(int i=0;i<s.length()-idx-1;i++)
    p*=10;
    int c=__gcd(a,p);
    a/=c;p/=c;
    cout<<a/p<<" "<<a-p*(a/p)<<"/"<<p;
}
Run
import java.util.*;
public class Main
{
  static long gcd(long a, long b)
{
    if (a == 0)
        return b;
    else if (b == 0)
        return a;
    if (a < b)
        return gcd(a, b % a);
    else
        return gcd(b, a % b);
}
     
public static void main(String[] args)
{
    Scanner sc=new Scanner(System.in);
   double number=sc.nextDouble(); 
   double intVal = Math.floor(number);
  
    double fVal = number - intVal;
  
    final long pVal = 1000000000;
  
    long gcdVal = gcd(Math.round(
                      fVal * pVal), pVal);
   
    long num = Math.round(fVal * pVal) / gcdVal;
    long deno = pVal / gcdVal;
  
    System.out.println((int)number+" " +
                           num + "/" + deno);
}
}
Run
import math
s=input()
s1=""
idx=-1
for i in range(len(s)):
    if s[i]=='.':
        idx=i
    else:
        s1+=s[i]
a=int(s1)
p=1
if idx!=-1:
    for i in range(len(s)-idx-1):
        p*=10
c=math.gcd(a,p)
a//=c
p//=c
s=str(a//p)+" "+str(a-p*(a//p))+"/"+str(p)
print(s)

Question 7

Given a sentence with numbers representing a word’s location in the sentence, embedded within each word, and return the sorted sentence. 

Note: We are using a maximum of 0-9 numbers only for 1 sentence

Example 1

  • Input:  is1 Thi0s T3est 2a
  • Output:  This is a Test

Example 2

  • Input:  t2o j3oin 4WonderBiz I0 Technolog5ies wan1t
  • Output:  I want to join WonderBiz Technologies
Run
#include<bits/stdc++.h>
using namespace std;
map<int,string> m;
 
void fun(string s)
{
    string s1="",s2="";
    for(auto i:s)
    {
        if(i<='9'&&i>='0') s1+=i;
        else s2+=i;
    }
    m[stoi(s1)]=s2;
}
 
int main()
{
    string s;
    int c=0;
    getline(cin,s);
    istringstream ss(s);
    while(ss)
    {
        string word;ss>>word;
        if(word=="") break;
        fun(word);
        c++;
    }
    for(int i=0;i<c;i++)
    cout<<m[i]<<" ";
}
Run
import java.util.*;
public class Main
{
  public static void main(String[] args)
{
   Scanner sc=new Scanner(System.in);
   String str=sc.nextLine();
   String[] arr=str.split(" ");
  String[] res=new String[arr.length];
  
for(int i=0;i<arr.length;i++)
{
   for(int j=0;j<arr[i].length();j++) { if(arr[i].charAt(j)>='0' && arr[i].charAt(j)<='9')
    {
      res[Integer.parseInt(arr[i].charAt(j)+"")]=arr[i].substring(0,j)+arr[i].substring(j+1,arr[i].length());
       break;
     }
}
}
String temp="";
for(int i=0;i<res.length;i++)
  temp=temp+res[i]+" ";
System.out.println(temp);
}
}
Run
from collections import defaultdict
m=defaultdict(str)
s=list(map(str,input().split(" ")))
for i in s:
    s1=""
    s2=""
    for j in i:
        if j<='9' and j>='0':
            s1+=j
        else:
            s2+=j
    m[int(s1)]=s2
for i in range(len(s)):
    print(m[i],end=" ")

Question 8

Write a program that takes an integer M and M integer array elements as input. The program needs to find the minimum difference between two elements in the integer array. The program then needs to print all those pairs of elements that have the minimum difference. If more than one pair has the minimum difference, then the program should print the output in the ascending order, if an element exists in two or more pairs, then it should be printed two times or more.

Example 1

  • Input: 4
               55 44 33 22
  • Output:  22 33 33 44 44 55
  • Explanation: The minimum difference between two elements is 11. Hence the pairs are printed in the ascending order. Here 33 and 44 appear in two different pairs; hence both are printed twice.

Example 2

  • Input: 5
               1 99 22 44 1001
  • Output:  1 22
Run
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    int n;cin>>n;vector a(n);
    int mm=INT_MAX;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a.begin(),a.end());
    map<int,vector<pair<int,int>>> m;
    for(int i=1;i<n;i++)
    {
        mm=min(mm,a[i]-a[i-1]);
        m[a[i]-a[i-1]].push_back({a[i],a[i-1]});
    }
    for(auto i:m[mm])
    cout<<i.second<<" "<<i.first<<" ";
}

Run

import java.util.*;
public class Main
{

 static int findMinDiff(int[] arr, int n)
    {
           Arrays.sort(arr);
             int min = Integer.MAX_VALUE;
             for (int i=0; i<n-1; i++)
              if (arr[i+1] - arr[i] < min)
                  min = arr[i+1] - arr[i];
          return min;
    }
  public static void main(String[] args)
{
  Scanner sc=new Scanner(System.in);
  int n=sc.nextInt();
  int arr[]=new int[n];
  for(int i=0;i<n;i++)
    arr[i]=sc.nextInt();
  
  int min=findMinDiff(arr,n);
   for(int i=0;i<n-1;i++)
     if (arr[i+1] - arr[i] ==min)
       System.out.print(arr[i]+" "+arr[i+1]+" ");
}
}
Run
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
a=sorted(a)
m=defaultdict(list)
mm=a[1]-a[0]
for i in range(1,n):
    mm=min(mm,a[i]-a[i-1])
    m[a[i]-a[i-1]].append([a[i],a[i-1]])
for i in m[mm]:
    print(i[1],i[0],end=" ")

Question 9

Write a program to convert a number to binary format.

Example 1

  • Input:  0
  • Output:  0

Example 2

  • Input:  1
  • Output:  1 
Run
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    int n;cin>>n;
    string s="";
    while(n)
    {
        if((n&1)) s+='1';
        else s+='0';
        n>>=1;
    }
    reverse(s.begin(),s.end());
    cout<<s;
}
Run
import java.util.*;
public class Main
{
static void toBinary(int n)
    {
        int[] binary = new int[32];
       int temp=n;
        int i = 0;
        while (temp> 0) {
            binary[i] = temp % 2;
            temp = temp/ 2;
            i++;
        }
    if(n==0)
    System.out.println("0");
    else 
  {    
    for (int j = i - 1; j >= 0; j--)
            System.out.print(binary[j]);
}
  }
 
  public static void main(String[] args)
{
  Scanner sc=new Scanner(System.in);
  int n=sc.nextInt();
  toBinary(n);
}
}
Run
n=int(input())
s=""
while n:
    if (n&1):
        s+='1'
    else:
        s+='0'
    n>>=1
print(s[::-1])

Question 10

Write a program that receives a word A and some texts as input. You need to output the texts (without modifying them) in the ascending order of the number of occurrences of the word A in the texts. The input is as follows: an integer M(between 1 and 100, inclusive), followed by the word A in the next line, and some text in each of the M next lines.

Note: The texts and the word A contain only lowercase Latin letters (a,b,c…,z) and blank spaces (“ ”). The maximum size of the texts and the word A is 100 Characters. Every text has a different number of occurrences of the word A.

Note 2:you must print one text per line without modifying the texts.

Example 1

  • Input: 2
               Java
                I hate java 
                Python is a good programming language
  • Output: Python is a good programming language
                  I hate java

Example 2

  • Input:  3
                python
                I like to code in python
                python is named after a show name monty python and not after the snake python
                I think python is good i think python is important than php
  • Output: i like to code in python
                  i think python is good i think python is important than php
                  python is named after a show name monty python and not after the snake python

Run

#include<bits/stdc++.h>
using namespace std;
string s;
map<string,int> m2;
bool cmp(pair<string, int>& a,
         pair<string, int>& b)
{
    return a.second < b.second;
}
void sort(map<string, int>& M)
{
    vector<pair<string, int> > A;
    for (auto& it : M) {
        A.push_back(it);
    }
    sort(A.begin(), A.end(), cmp);
    for (auto& it : A) {
        for(int i=0;i<m2[it.first];i++)
        cout << it.first <<endl; } } int count(string s1) { istringstream ss(s1); int c=0; while(ss) { string w; ss>>w;
        if(w==s) c++;
    }
    return c;
}
int main()
{
    int n;getline(cin,s);n=stoi(s);
    getline(cin,s);
    transform(s.begin(),s.end(),s.begin(),::tolower);
    vector<string> v(n);
    vector<int> a(n);
    map<string,int> m;
    for(int i=0;i<n;i++)
    {
        getline(cin,v[i]);
        transform(v[i].begin(),v[i].end(),v[i].begin(),::tolower);
        m2[v[i]]++;
        m[v[i]]=count(v[i]);
    }
    sort(m);
}
Run
import java.util.*;
public class Main
{

static int countOccurences(String str, String word)
{
    String a[] = str.split(" ");
 
    int count = 0;
    for (int i = 0; i < a.length; i++)
    {
      if (word.equals(a[i]))
        count++;
    }
 
    return count;
}
  public static void main(String[] args)
{
  Scanner sc=new Scanner(System.in);
  int n=sc.nextInt();
 sc.nextLine();
 String word=sc.next(); 
 //System.out.println(word);
 sc.nextLine();
 String arr[]=new String[n];
 for(int i=0;i<n;i++)
{
  arr[i]=sc.nextLine();
  //System.out.println(arr[i]); 
}
 TreeMap<Integer,String> map=new TreeMap<Integer,String>();	
  for(int i=0;i<n;i++)
{
   map.put(countOccurences(arr[i],word),arr[i]);
}

Set s=map.entrySet();
Iterator itr=s.iterator();
while(itr.hasNext())
{
  Map.Entry m=(Map.Entry)itr.next();

  System.out.println(m.getValue());
}

}
}
Run
from collections import defaultdict
d=defaultdict(int)
d1=defaultdict(int)
n=int(input())
s=input()
s=s.lower()
l=[]
for i in range(n):
    L=list(map(str,input().split()))
    s1=' '.join(i for i in L)
    c=0
    for j in L:
        if s==j:
            c+=1
    l.append(s1)
    d[i]=c
    d1[i]+=1
    c=0
d=sorted(d.items(),key=lambda a:a[1])
for i in d:
    for j in range(d1[i[0]]):
        print(l[i[0]])