Monday, 14 December 2020

Zoho interview Question-Path

Question

Find the Path Given an (m x n) matrix, write a program to traverse the cell and print the values present in the given path. Inclued necessary validation and proper error messages in case of given path is out of bounds. 5 x 5 matrix : {1 2 3 4 5 } (row 1) {6 7 8 9 0 } (row 2) {1 2 3 4 5 } (row 3) {6 7 8 9 0 } (row 4) {1 2 3 4 5 } (row 5) Path Notation : ‘’>” is going right, “v” going down, “<” is going left, “^” is going up


Answer

 import java.util.Scanner;


class FindPath {


public static void main(String[] args) {

int row,col,startRow,startCol,length=0;

String path,output="";

Scanner scanner=new Scanner(System.in);

row=scanner.nextInt();

col=scanner.nextInt();

int matrix[][]=new int[row][col];

for(int i=0;i<row;i++)

{

for(int j=0;j<col;j++)

{

matrix[i][j]=scanner.nextInt();

}

}

startRow=scanner.nextInt();

startCol=scanner.nextInt();

scanner.nextLine();

path=scanner.nextLine();

if(row>startRow-1&&col>startCol-1)

{

startRow--;

startCol--;

output+=matrix[startRow][startCol]+"  ";

for(length=0;length<path.length();length++)

{

if(path.charAt(length)=='>'&&startCol+1<col)

{

output+=matrix[startRow][startCol+1]+"  ";

startCol++;

}

else if(path.charAt(length)=='<'&&startCol-1>=0)

{

output+=matrix[startRow][startCol-1]+"  ";

startCol--;

}

else if(path.charAt(length)=='^'&&startRow-1>=0)

{

output+=matrix[startRow-1][startCol]+"  ";

startRow--;

}

else if((path.charAt(length)=='v'||path.charAt(length)=='V')&&startRow+1<row)

{

output+=matrix[startRow+1][startCol]+"  ";

startRow++;

}

else

{

System.out.println("Invalid path");

output="";

break;

}

}

}

else

{

System.out.println("Invalid path");

}

System.out.println(output);


}


}

Example

inputs

5 5

1 2 3 4 5 
6 7 8 9 0 
1 2 3 4 5 
6 7 8 9 0 
1 2 3 4 5
 2 3

 v>>v<<^>>vv

output


8 3 4 5 0 9 8 3 4 5 0 5 

Zoho Programming question -Locker Problem

 Question

Locker Problem There is a school with 100 students, and correspondingly 100 lockers, all of which start off closed. The first student opens every locker. The second student closes every other locker, starting with the second (2, 4, 6 etc). The third student changes the state of every third locker starting with the third (3,6,9 etc). The fourth would change the status of lockers numbered 4,8,12 etc.,. That is, if the locker is open, it is closed, and if it is closed, it is opened. This continues until all 100 students have passed along the lockers. After the 100th student is done, which lockers are open and which are closed? [Note: program should work for any number of students/lockers]

Logic:

1 4 9 16...

every perfect square will b in open. 


Solution:

import java.util.Scanner;


class Locker {


public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

int lockerCount,index=2,square=1,open=0;

lockerCount=scanner.nextInt();

while(square<=lockerCount)

{

open++;

square=index*index;

index++;

}

System.out.println("open count"+open+"        close count"+(lockerCount-open));

}


}

Example


100 

open = 10 close = 90 

6456 
open = 80 close = 6376 

Tuesday, 1 December 2020

Zoho 1st round Questions

helloo guys after lockdown this interview process happens through online. i took snap shot of technical questions and i wish to share to all of u... 

1.int main(int argc, char *argv[])

{

  union check

  {

      int i;

      int j;

  };

  union check uij;

  uij.i=10;

  uij.j=20;

  if(uij.i>0){

      printf("%d %d",uij.i,uij.j);

  }else{

      printf("Zoho Interview");

  }

    return 0;

}


Output : 20 20


Since union share the same memory location for both i & j last stored value will be  displayed.

2)int main(int argc, char *argv[])

{

 int i=0;

 if(+(+i--)!=0){ //-1

     i-=i++;  //-1-0

 }else{

     i-=i--;

 }

 printf("%d",i);

    return 0;

}


ans  -1


3)


#include <stdio.h>


int main(int argc, char *argv[]){

int tally;

char p[]="zoho";

char t;

int i,j;

for(i=0,j=strlen(p);i<j;i++){

    t=p[i];

    p[i]=p[j-i];

    p[j-i]=t;

    printf("%c %c \n",t,p[i]);

}

    return 0;

}


ans : 

z

o o 

h h 

o o

4)

int main(int argc, char *argv[]){

int c[]={2.8,3.4,4,6.7,5};

int j, *p=c, *q=c;

for(j=0;j<5;j++){

    printf("%d",*c);//here it called the c[0] 5 times c didnnt get incremented

    ++q;

}

for(j=0;j<5;j++){

    printf("%d",*p); // here p gets incremented

    ++p;

}

    return 0;

}


ans : 2222223465

5)final class GetValues{

    private final double im;

    public GetValues(double im){

        this.im=im;

    

    }

    public String toString(){ // over ridding

        return "(Value is:"+im+")";

    }

}

public class Main

{

public static void main(String[] args) {

    GetValues c=new GetValues(10);

System.out.println(c);// when we call object it will call toString() method

}

}


ans : value is:10.0

6)

public class Main

{

    public static void swap(Integer i,Integer j){

        Integer temp=new Integer(i);

        i=j;

        j=temp;

        System.out.println("Swapped:"+"i="+i+",j="+j);

    }

public static void main(String[] args) {

    Integer i=new Integer(10);

    Integer j=new Integer(20);

    swap(i,j);

System.out.println("final:"+"i="+i++ +",j="+j++);

}

}


Ans : swapped:i=20,j=10

final:i=10,j=20


7)

public class Main

{

public static void main(String[] args) {

    final String testString="Zoho";

    testString.chars().forEach(ch->System.out.print(ch)); //stream gives integer as output

}

   

}


ans : 90111104111

8)

public class Main

{

public static void main(String[] args) {

  int x=0;

  while(1) // is not allowed should contains boolean alone

  {

      x+=1;

      if(x==3){continue;}

      if(x==5)break;

      System.out.println("x:"+(x));

  }

}

   

}


ans : compilation error

9)public class Main

{

public static void main(String[] args) {

  int currentStatus=0;

  stateOne:

  {

  if(8!=15>>1){

      currentStatus=1;

      stateTwo:

      {

          if (5==10>>1){

              currentStatus=2;

              break stateOne;  // break the entire block

          }

          break stateTwo;

      }

      currentStatus=-1;

  }

  }

  System.out.println(currentStatus);

}

}


ans : 2

10)

import java.util.TreeSet;

public class Main

{

public static void main(String[] args) {

TreeSet alphabetStrings=new TreeSet<>(String::compareTo);


alphabetStrings.add("Zebra");

alphabetStrings.add("Apples");

alphabetStrings.add("Ball");

alphabetStrings.add("zebra");

alphabetStrings.add("apple");

alphabetStrings.add("ball");

  System.out.println("AlphabetString Set contains:"+alphabetStrings);

}

}


ans : AlphabetString Set contains:[Apples, Ball, Zebra, apple, ball, zebra]

11)import java.util.*;
public class Main
{
public static void main (String[] args)
{
    int arr1[]={1,2,3};
    int arr2[]=arr1;
    if (Arrays.equals(arr1,arr1.clone()))
    System.out.println("Same");
    else
    System.out.println("Not Same");
    if(Arrays.equals(arr1,arr2))
     System.out.println("Same");
    else
    System.out.println("Not Same");
}
 
}

ans : Same
Same
12)
public class Main
{
public static void main (String[] args)
{
   String str="zoho";
   str.toUpperCase();
   str+="onzoho";
   String string = str.substring(2);
   string = string + str.substring(10);
   System.out.println(string);
}
 
}
ans : hoonzoho

13)
import java.util.stream.*;
public class Main
{
public static void main (String[] args)
{
  Stream.of("Apples","Zebra","apples","zebra")
  .sorted()
  .map(s->{
      return s.toUpperCase();
  })
  .anyMatch(s->{
      System.out.println("String Match Condition:"+s);
      return s.startsWith("Z");
     
  });
}
 
}

ans : String Match Condition:APPLES                                                                                                                                                      
String Match Condition:ZEBRA

14)
class Base extends Exception {}
class Derived extends Base {}
public class Main
{
public static void main (String[] args)
{
  try{
      throw new Derived();
  }
  catch(Base b){    //parent class catch hence child case exception does not required
      System.out.println("Caught base class exception");
  }
  catch(Derived d){
      System.out.println("Caught derived class exception");
  }
}
 
}

Compiler error
15)
class Derived
{
    public void methodInvoke() //privileges public to protected not allowed
    {
        System.out.println("Derived Class");
    }
}
public class Main extends Derived
{
    protected void methodInvoke()
    {
        System.out.println("Test Class");
    }
public static void main (String[] args)
{
  Derived obj=new Main();
  obj.methodInvoke();
}
 
}
compiler error

16)
interface InterF {
    default void implMethod()
    {
        System.out.println("Base Method");
    }
}
class Test implements UbterF {
    public void implMethod() {
        System.out.println("Override Method");
    }
}
public class TestClass
{
    public static void main(String[] args)
    {
        InterF interF=new Test();
        interF.implMethod();
    }
}

ans : Override Method
17)
public class Main
{
    interface InterF{
        int getRes(int a,int b);
    }
    public static int printRes(int a,int b,InterF interF){
        return interF.getRes(b,a);
    }
    public static void main(String[] args)
    {
        InterF interF=(a,b)->a%b;
        System.out.println(printRes(20,30, interF));
    }
}
ANS 10

18)

import java.util.TreeSet;
import java.util.stream.Stream;

public class Main
    {
        public static void main(String args[])
        {
            Stream.of(1,-2,3)
            .sorted((s1,s2)->{
                System.out.printf("Sort Compare:%s;%s\n",s1,s2);
                return s1.compareTo(s2);
            
            })
            .filter(s->{
                return s>0;
                })
                .map(s->{
                    return s.doubleValue();
                })
                .forEach(s->System.out.println("Printed Val:"+s));
    }
}

ans : 

Sort Compare:-2;1                                                                                                                                                                  
Sort Compare:3;-2                                                                                                                                                                  
Sort Compare:3;1                                                                                                                                                                   
Printed Val:1.0                                                                                                                                                                    
Printed Val:3.0

19)

class Base{
    Base(){
        System.out.println("Base");
    }
    public void Print() {
        System.out.println("BasePrint");
    }
}
class Derived extends Base {
    public void Print() {
        System.out.println("DerivedPrint");
    }
}
class Main {
    public static void DoPrint(Base o) {
        o.Print();
    }
    public static void main(String[] args) {
        Base y=new Derived();
        Derived z=new Derived();
        DoPrint(y);
        DoPrint(z);
    }
}

ans : 

Base                                                                                                                                                                               
Base                                                                                                                                                                               
DerivedPrint                                                                                                                                                                       
DerivedPrint
220)
import java.util.*;
class Variable implements Comparable<Variable>{
    int id;
    int quantity;
    public Variable(int id,int quantity){
        this.id=id;this.quantity=quantity;
    }
    public int compareTo(Variable b){
        if(id>b.id){
            return -1;
        }else if(id<b.id){
            return 1;
        }else{
            return 0;
        
        }
    }
}
public class Main {
    public static void main(String[] args) {
Queue<Variable> queue=new PriorityQueue();
        Variable v1=new Variable(1,100);
        Variable v2=new Variable(2,1000);
        Variable v3=new Variable(3,500);
        Variable v4=new Variable(4,10);
        queue.add(v1);
        queue.add(v2);
        queue.add(v3);
        queue.add(v4);
        queue.remove();
        System.out.println(queue.peek().id+":"+queue.peek().quantity);
    }
}

ans : 3:500
21)

class PrivClass
{
    private PrivClass()
    {
        System.out.println("PrivClass called");
    }
    protected static PrivClass create()
    {
        PrivClass obj = new PrivClass();
        return obj;
    }
    public void myMethod()
    {
        System.out.println("PrivClass Method called");
    }
}
public class Main
{
    public static void main(String[] args)
    {
        try{
            PrivClass obj=PrivClass.create();
            obj.myMethod();
        }catch (Exception e){
        System.out.println("PrivClass Creation Error");
    }
}
}

ans : 

PrivClass called                                                                                                                                                                   
PrivClass Method called 
22)

import java.io.*;
public class Main {
    {
        public static void main(String[] args)
        {
            try
            {
                MyVariable object1=new MyVariable(Integer.MAX_VALUE+1);
                FileOutputStream fos=new FileOutputStream("serial");
                ObjectOutputStream oos=new ObjectOutputStream(fos);
                oos.writeObject(object1);
                oos.flush();
                oos.close();
            }
            catch(Exception e)
            {
                System.exit(0);
            }
            try
            {
                MyVariable object2;
                FileInputStream fis=new FileInputStream("serial");
                ObjectInputStream ois=new ObjectInputStream(fis);
                object2=(MyVariable)ois.readObject();
                ois.close();
                System.out.println(object2);
            }
            catch (Exception e)
            {
                System.exit(0);
            }
        }
    }
    class MyVariable implements Serializable
    {
        int i;
        MyVariable (int i)
        {
            this.i=i;
        }
        public String toString() {
            return ""+(i/1000)+"";
        }
    }
}

ans : -2147483
23)

class Test
{
    int a;
    
    Test cloning()
    {
        try
        {
            return (Test) super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            System.out.println("CloneNotSupportedException is caught");
            return this;
        }
    }
    public String toString() {
        return a+"";
    }
}
class Main
{
    public static void main(String args[])
    {
        Test obj1=new Test();
        Test obj2;
        obj1.a=10;
        obj2=obj1.cloning();
        obj2.a=20;
        System.out.println("obj1.a="+obj1.a+","+"obj2.a="+obj2.a);
    }
}

ans : 


CloneNotSupportedException is caught                                                                                                                                               
obj1.a=20,obj2.a=20