Download as pdf
Download as pdf
You are on page 1of 24
5/12/22, 1:03 PM ZOHO INTERVIEW QUESTIONS 1, Return two prime numbers Given an even number (greater than 2), return two prime numbers whose sum will be equal to given number? There are several combinations possible. Print only first such pair. NOTE: A solution will always exist, read Goldbach’s conjecture. Also, solve the problem in linear time complexity, Le., O(n). Input: The first line contains T, the number of test cases. The following T lines consist of a number each, for which we'll find two prime numbers. Note: The number would always be an even number. Output: For every test case print two prime numbers space separated, such that the smaller number appears first. Answer for each test case must be in a new line, Constraints: 1sT<70 1 hm=new HashMap(); for(int i=O;i0) { int nzinteger.parseint(br.readLine()}; int af}=new int{n}; String line=br.readLine(); String s{]=line.trim().split("\\s#"); for(int i=O;ienji++) { a[i]=Integer.parseint(s[i]); } freqla.n); y 4, Reverse each word in a given string Given a String of length N reverse each word in it. Words are separated by dots. Input: The first line contains T denoting the number of testcases. Then follows description of testcases. Each case contains a string containing dots and characters. Output: For each test case, output a String in single line containing the reversed words of the given String. 3124 5/12/22, 1:03 PM Constraints: 10){ String inp = sc.next(); /fSystem.out.printin(inp); Stringf] input = inp.split{"\\."); //System.out.printin(input.length); for(int i-O;i0){ hitps:toszinfldoc-view PDFs viewer int a=sc.nextint(); int b=sc.nextint(); int{][] inp=new int[a][b]; for(int i=O;icari++){ For{int j=0;j 80) return tru int |= 1%9; if (arr{k][l] == 0 && i <=80) { for{int m=1; m<=9; m+4) { iflisSafe(arr,k,Lm)){ are{kjllJ=m; /iSystemn.out.format("Setting value %d 96d 96d\n",, 1, mn); iffk && arr{k}(l] arr.length-1 && | M for(int c=0; c0){ int n = sc.nextint(); int{] @ = new intin]; for(int i=Ojienji+4}{ ali] = se.nextint(); } forint i=0;icn;i++){ int diff = Integer.MAX_VALUE; 4; forfint j-O;jenji++){ if (afi) < afj] && Li] - ali] < diff yy diff = afi] - ali]; closest sti int closest } if(closes 1) System.out.print( "_ else System.out.print(a[closest] + *"); hitps:toszinfldoc-view PDFs viewer System.out.printin(); 11. Total count Given an array of positive integers and a number K where K is used as threshold value to divide each element of the array into sum of different numbers. Find the sum of count of the numbers in which array elements are divided. Input: ‘The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N and K, where Nis the size of array and Kis the threshold value. The second line of each test case contains N space separated integers denoting array elements. Output: Corresponding to each test case, print the total count. Constraints: 1sTs100 10) { intn Integer. parseint{in.readLine()); int arf] = new int{n); StringTokenizer st = new StringTokenizer(in.readLine()); for(int temp=0;tempen;temp++) { ar[temp]=Integer.parselnt(st.nextToken()}; } long ans=Integer.MIN_VALUE; long cursum=0; for(int temp=0;temp0) int n1=sc.nextint(); int n2=se.nextint(); int arr{]=new int{na}; int arr2{]=new int{n2}; for(int f=0;fn2) int arr3[]=new int[n1]; else int arr3{}=new int[n2];*/ Mint q=n1>n2?n1:n2; J System.out.printin(n t+" “an2+" "+q); Jf int arr3{}=new int{g+1]; // int k=arr3.length; “ int ¢=0; ArrayList arr3=new ArrayList(); Mint jon2-1; int ij; for(ient-t j=n2-1;i>=0&&j>= { int sum=arr[i]+arr2[j]+c; if(sum>=10) { arr3.add(sum%10); ch } else { arr3.add(sum); while(j>=0) { int sum=arr2{j]+c; iffsum>=10) £ arr3.add(sum%10); 13924 5/12/22, 1:03 PM } else{ arr3.add(sum); //system.out.printin("*"}; while(i>=0) { //System.out.printin( ff arr3.add{arrd[il+c); int sum=arr1[i]+c; if(sum>=10) { arr3.add(sum%10); c=; } else{ arr3.add(sum); ifle { arr3.add(1); } //ArratList arr4=arr3.reverse(); hitps:toszinfldoc-view PDFs viewer Collections.reverse(arr3); forlint k=0;k0) { String inp=sc.next(); int n=se.nextint(); // System.out.printin(inp+"Hi"+n); int{] ar=new int(26]; int count: char tem} forint i=0;i=0) 16124 5/12/22, 1:03 PM { //system.out.printin(97-(int)inp.charAtli)); ar[(int)inp.charAt(i)-97]++; temp= inp.charAtli); } /fystem.out.printin(ar.length); for(int izO;i0) { int r= % 10; sum = sum * 10 +5; n=n/10; } return sum; 18124 5/12/22, 1:03 PM public static void main (String|] args) { Scanner scan = new Scanner(System.in); int t= scan.nextint(}; while(t>0) { int orgnum = scan.nextint(); GFG obj = new GFGI); int revnum = obj.reverse(orgnum); System.out.printin(orgnum); } else { int ressum. =0; for(int i=0;i<5;i++) { // int ressum = orgnum + revnum; result = obj.reverse(ressum); rgnum + revnum; int resul if(result == ressum) { /fsystem.out.printin(result); break; } else { ressum = result + ressum; // result = this.reverse(ressum); } System.out.printin(result); } else { System.out.printin("-1"); hitps:toszinfldoc-view PDFs viewer 20. Total Decoding Messages A top secret message containing letters from A-Zis being encoded to numbers using the following mapping ‘Nol 'Bi>2 726 You are an FBI agent. You have to determine the total number of ways that message can be decoded. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0's, extra trailing 0's and two or more consecutive 0's then it is an invalid string Example : Given encoded message "123", it could be decoded as "ABC" (1.2 3) or "LC" (12 3) or "AW"(1 23). So total ways are 3. Input: First line contains the test cases T. 1< Each test case have two lines First is length of string N. 1<=N<=40 Second line is string $ of digits from '0' to'S' of N length. Example: Input: 2 3 123 4 2563 Output: 3 2 Solution 19124 5/12/22, 1:03 PM import java.util.*; import java.lang.*; import java.io.*; class GFG [ static int solve(String str) { if(str.tength| { 0) return 1; } if(str.charAt(O) { return 0; } if(str.length() { 1) return 1; int count=0; if(str.charAt(0}>50) { count=count#solve(str.substring(1)); } if(str-length()>1&&str.charAt( { if(str.charat(a) { count=count#solve(str.substring(2)); } else { count=count+solve(str-substring(1)}+solve(s tresubstring(2)); hitps:toszinfldoc-view PDFs viewer iffstr.length()>18&&str.charAt(0}=="2') { if{str.charAt(1)>=488str.charAt(1)<=54) { if(ste.charAt(1)=='0') { count=count#solve(str.substring(2)); } else { count=count#solve(str.substring(1)}+solve(s tr-substring(2)); else count=count+solve(str.substring(1)}; } } return count; } public static void main (String{] args) { I{code Scanner s=new Scanner(System.in); int t=s.nextint(); while(t>0) { int n=s.nextint(); String str=s.next(); int count=solve(str); iffcount<0) { System.out.printin(0); } else { System.out.printin(count); 20124 5/12/22, 1:03 PM e 21. Cross character Convert a given string to its cross string (i.e Diagonal from left-right and from right-left) Input: The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of a String in lower case. Output: Print given string(in lower case) in cross manner. Constraints: 1sT<100 1s length of string s 50 Example: Input 2 geeks geeksforgeeks Output g sek e ekg s ge se ke eke se f roo Ff ro osg kee ee kg 5 Explanation: For the ‘st test case where string is geeks Gs EK E EK Gs hitps:toszinfldoc-view PDFs viewer The above is the proper cross manner for the test. case, but when printed in a single line it becomes as shown in the output Solution import java.util.*; import java.lang.*; import java.io. class GFG { public static void main (Stringl] args) { Scanner sc=new Scanner(System.in}; int i.k,Ln; n=sc.nextint(); se.nextLine(); forli=O;icn;it+) { StringBuilder op=new StringBuilder(); String str=sc.nextLine(); int len=str.length(); forlj=0;j=0;)--) { for(|=0;l=(j*2)42;k~) op.append(" op.append(str.charAt((len-1)-))); for(l=0;1=m| |e=n) { break; } if(ch{r][¢]!=s.charat(k)) { break; } r+=x[val]; cteylval]; } iflk { return true; length()) } return false; } public static void display(char{]{] ch,String s) { hitps:toszinfldoc-view PDFs viewer int flag=0; for(int row=0;row0){ intn= Integer.parseint(br.readLine().trim()); ifl(n&1)==0) ‘System.out.printin{(n>2)?"Yes":"No"); else if(isPrime(n-2)) System.out.printin("Yes"); else System. out.printin("No" } br.close(); } 24i24

You might also like