Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,107 views
in Interview-Experiences by Expert (30,360 points) | 1,107 views

2 Answers

0 like 0 dislike
Best answer

These were the questions asked for the GS coder pad (1st round)-

 

  1. Compress String - aabbc - a2b2c1, a - a1
  2. Find optimal path -
    Grid - [[0, 0, 0, 0, 5],
    [0, 1, 1, 1, 0],
    [2, 0, 0, 0, 0]]

 

Start is [2, 0], destination - [0, 4]
Can only go up or east(right)
Numbers denote the rocks, find the most optimal path where you go from source to destination and collect the maximum number of rocks in the process.
Output - 10

by Expert (30,360 points)
0 like 0 dislike
Code in Java(Recursive)
public class OptimalPath {
public static void main(String[] args) {
int[][] grid={{0,0,0,0,5} ,{0,1,1,1,0} ,{2,0,0,0,0}};
maxPath(grid ,2,0,0,4);
System.out.println( maxPath(grid ,2,0,0,4));
}
static int maxPath(int[][] grid , int sr, int se,int ds,int de){
System.out.println(sr+" "+se+" "+ds+" "+de);
if(ds==sr && de==se){
return grid[ds][de] ;
}
if(ds> grid.length -1|| de > grid[0].length-1 && ds < 0|| de < 0)
return 0;
int up=grid[ds][de]+maxPath(grid,sr,se,ds+1,de);
int right=grid[ds][de]+maxPath(grid,sr,se,ds,de-1);

    return Math.max(up ,right);
    //return up+right;
}
}
by Expert (30,360 points)