├── Q01.java ├── Q02.Java ├── Q03.java ├── Q04.java ├── Q05.java ├── Q06.java ├── Q07.java ├── Q08.java ├── Q09.java ├── Q10.java ├── Q11.java ├── Q12.java ├── Q13.java ├── Q14.java ├── Q15.java ├── Q16.java ├── Q17.java ├── Q18.java ├── Q19.java ├── Q20.java ├── Q22.java ├── Q24.java ├── Q25.java ├── Q27.java ├── Q30.java ├── Q32.java ├── Q36.java ├── README.md ├── Train.java └── taxi.java /Q01.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //Print the word with odd letters as 4 | //I/O : PROGRAM 5 | //O/P 6 | //P M 7 | // R A 8 | // O R 9 | // G 10 | // O R 11 | // R A 12 | //P M 13 | public class Q1 { 14 | public static void main(String[] args) { 15 | Scanner sc = new Scanner(System.in); 16 | String S = sc.nextLine(); 17 | if(S.length()%2!=0) { 18 | int space = S.length()-2; 19 | for(int i=0,j=S.length()-1;i<(S.length()/2);i++,j--) { 20 | for (int x=0; x=0;i--,j++) { 34 | for (int x=i; x>0; x--) 35 | System.out.print(" "); 36 | System.out.print(S.charAt(i)); 37 | System.out.format("%1$"+space+"s", ""); 38 | System.out.println(S.charAt(j)); 39 | space = space+2; 40 | } 41 | } 42 | else { 43 | System.out.println("Not of odd length"); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Q02.Java: -------------------------------------------------------------------------------- 1 | //Alternate sorting: Given an array of integers, rearrange the array in such a way that the 2 | //first element is first maximum and second element is first minimum. 3 | // 4 | // Eg.) Input : {1, 2, 3, 4, 5, 6, 7} 5 | // Output : {7, 1, 6, 2, 5, 3, 4} 6 | 7 | import java.util.Scanner; 8 | import java.util.Arrays; 9 | 10 | public class Q2 { 11 | 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | int n = sc.nextInt(); 15 | int arr[] = new int[n]; 16 | for(int i=0;i0) 15 | { 16 | return a+multiply(a,--b); 17 | } 18 | else 19 | { 20 | a=-a; 21 | b=-b; 22 | return multiply(a,b); 23 | } 24 | } 25 | public static void main(String[] args) { 26 | Scanner sc = new Scanner(System.in); 27 | int a = sc.nextInt(); 28 | int b = sc.nextInt(); 29 | 30 | System.out.print(multiply(a,b)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Q04.java: -------------------------------------------------------------------------------- 1 | //Given a 9×9 sudoku we have to evaluate it for its correctness. We have to check both the sub matrix correctness and the whole sudoku correctness. 2 | ``` 3 | 5 3 4 6 7 8 9 1 2 4 | 6 7 2 1 9 5 3 4 8 5 | 1 9 8 3 4 2 5 6 7 6 | 8 5 9 7 6 1 4 2 3 7 | 4 2 6 8 5 3 7 9 1 8 | 7 1 3 9 2 4 8 5 6 9 | 9 6 1 5 3 7 2 8 4 10 | 2 8 7 4 1 9 6 3 5 11 | 3 4 5 2 8 6 1 7 9 12 | 13 | 14 | 3 1 6 5 7 8 4 9 2 15 | 5 2 9 1 3 4 7 6 8 16 | 4 8 7 6 2 9 5 3 1 17 | 2 6 3 4 1 5 9 8 7 18 | 9 7 4 8 6 3 1 2 5 19 | 8 5 1 7 9 2 6 4 3 20 | 1 3 8 9 4 7 2 5 6 21 | 6 9 2 3 5 1 8 7 4 22 | 7 4 5 2 8 6 3 1 9 23 | ``` 24 | 25 | import java.util.*; 26 | 27 | public class Q4 28 | { 29 | public static void main(String[] args) { 30 | Scanner sc = new Scanner(System.in); 31 | int[][] arr = new int[9][9]; 32 | 33 | for(int i=0;i<9;i++){ 34 | for(int j=0;j<9;j++){ 35 | arr[i][j]=sc.nextInt(); 36 | } 37 | } 38 | 39 | int count; 40 | 41 | for(int i=0;i<9;i++){ 42 | count=0; 43 | for(int j=0;j<9;j++){ 44 | count=count+arr[i][j]; 45 | } 46 | if(count!=45){ 47 | System.out.print("Sudoku is not correct"); 48 | System.exit(0); 49 | } 50 | } 51 | 52 | for(int j=0;j<9;j++){ 53 | count=0; 54 | for(int i=0;i<9;i++){ 55 | count=count+arr[i][j]; 56 | } 57 | if(count!=45){ 58 | System.out.print("Sudoku is not correct"); 59 | System.exit(0); 60 | } 61 | } 62 | for(int i=0;i<3;i++) 63 | { 64 | for(int j=0;j<3;j++) 65 | { 66 | count=0; 67 | for(int k=0;k<3;k++) 68 | { 69 | for(int l=0;l<3;l++) 70 | count=count+arr[i*3+k][j*3+l]; 71 | } 72 | 73 | if(count!=45) 74 | { 75 | System.out.print("Sudoku is not correct"); 76 | System.exit(0); 77 | } 78 | } 79 | 80 | 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Q05.java: -------------------------------------------------------------------------------- 1 | /* 2 | 2.Given sorted array check if two numbers sum in it is a given value 3 | Input 4 | Array = {1 3 4 8 10 } N = 7 5 | output 6 | true 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public class Q5 12 | { 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | int n = sc.nextInt(); 16 | int[] arr = new int[n]; 17 | for(int i=0;imax){ 25 | max=arr[i]; 26 | } 27 | } 28 | int[] tempArr = new int[max+1]; 29 | 30 | for(int i=0;i0){ 36 | System.out.println(i+"->"+tempArr[i]); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Q07.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an odd length word which should be printed from the middle of the word. 3 | 4 | Example: 5 | 6 | Input: PROGRAM 7 | The output should be in the following pattern: 8 | G 9 | GR 10 | GRA 11 | GRAM 12 | GRAMP 13 | GRAMPR 14 | GRAMPRO 15 | 16 | */ 17 | 18 | import java.util.*; 19 | 20 | public class Q7 21 | { 22 | public static void main(String[] args) { 23 | Scanner sc = new Scanner(System.in); 24 | 25 | String s = sc.next(); 26 | int space = (s.length()-1)*2; 27 | char[] arr = new char[s.length()]; 28 | 29 | int j = 0; 30 | for(int i=s.length()/2;i0) 42 | System.out.format("%1$"+space+"s", ""); 43 | for(int k=0;karr[i-1]){ 22 | System.out.println(arr[i]); 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Q09.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | You’re given a number n. 4 | If write all the numbers from 1 to n in a paper, we have to find the number of characters written on the paper. 5 | For example if n=13, the output should be 17 if n = 101, the output should be 195 6 | 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public class Q9{ 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | int n = sc.nextInt(); 15 | int count=0; 16 | int num; 17 | for(int i=1;i<=n;i++){ 18 | num=i; 19 | while(num!=0){ 20 | count++; 21 | num = num/10; 22 | } 23 | } 24 | System.out.print(count); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Q10.java: -------------------------------------------------------------------------------- 1 | /* 2 | 5.Write a program to print the below pattern 3 | 4 | for n = 6 5 | 1 7 12 16 19 21 6 | 2 8 13 17 20 7 | 3 9 14 18 8 | 4 10 15 9 | 5 11 10 | 6 11 | 12 | */ 13 | 14 | import java.util.*; 15 | 16 | public class Main 17 | { 18 | public static void main(String[] args) { 19 | Scanner sc = new Scanner(System.in); 20 | int n = sc.nextInt(); 21 | int count=n; 22 | int num; 23 | for(int i=1;i<=n;i++){ 24 | num=i; 25 | for(int j=0;j we want to find sum of weights based on the following conditions 3 | 1. 5 if a perfect square 4 | 2. 4 if multiple of 4 and divisible by 6 5 | 3. 3 if even number 6 | And sort the numbers based on the weight and print it as follows 7 | <10,its_weight>,<36,its weight><89,its weight> 8 | Should display the numbers based on increasing order. 9 | */ 10 | 11 | import java.util.*; 12 | 13 | public class Main 14 | { 15 | public static void main(String[] args) { 16 | Scanner sc = new Scanner(System.in); 17 | int n = sc.nextInt(); 18 | int[] arr = new int[n]; 19 | int[] arr1 = new int[n]; 20 | 21 | for(int i=0;i"); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Q12.java: -------------------------------------------------------------------------------- 1 | /* 2 | Input: 3 | Number of elements in set1: 4 4 | Elements are: 9, 9, 9, 9 5 | Number of elements in set 2: 3 6 | Elements are: 1,1,1 7 | Output: 8 | 1, 0, 1, 1, 0 9 | 10 | Input: 11 | Number of elements in set1: 11 12 | Elements are: 7,2,3,4,5,3,1,2,7,2,8 13 | Number of elements in set 2: 3 14 | Elements are: 1,2,3 15 | Output: 7,2,3,4,5,3,1,2,8,5,1 16 | */ 17 | 18 | import java.util.*; 19 | 20 | public class Main 21 | { 22 | public static void main(String[] args) { 23 | Scanner sc = new Scanner(System.in); 24 | int n,m; 25 | System.out.println("Number of elements in set1:"); 26 | n = sc.nextInt(); 27 | int[] arr1 = new int[n]; 28 | for(int i=0;im){ 40 | temp=n; 41 | } 42 | else{ 43 | temp=m; 44 | } 45 | 46 | int[] arrR = new int[temp+1]; 47 | int tempA,tempB,tempR=0; 48 | for(int i=temp;i>=0;i--){ 49 | if(n-1>=0){ 50 | tempA=arr1[n-1]; 51 | n--; 52 | } 53 | else{ 54 | tempA=0; 55 | } 56 | if(m-1>=0){ 57 | tempB=arr2[m-1]; 58 | m--; 59 | } 60 | else{ 61 | tempB=0; 62 | } 63 | arrR[i]=(tempA+tempB+tempR)%10; 64 | tempR = (tempA+tempB+tempR)/10; 65 | } 66 | if(arrR[0]!=0){ 67 | System.out.print(arrR[0]+","); 68 | } 69 | for(int i=1;ii){ 42 | arr[a][b]=num; 43 | b--; 44 | } 45 | while(a>i){ 46 | arr[a][b]=num; 47 | a--; 48 | } 49 | num--; 50 | size--; 51 | } 52 | 53 | for(int i=0;i=0;){ 22 | if(Character.isLetter(s.charAt(j))){ 23 | temp = temp+s.charAt(j); 24 | j--; 25 | } 26 | else { 27 | j--; 28 | } 29 | } 30 | int count=0; 31 | for(int i=0;i=i) { 45 | arr[i1][j1]=m; 46 | j1--; 47 | } 48 | j1++; 49 | while(i1>=i) { 50 | arr[i1][j1]=m; 51 | i1--; 52 | } 53 | j1++; 54 | m--; 55 | } 56 | 57 | for(int i=0;i=0;i--) { 31 | // result +=(arr[i]+" "); 32 | // } 33 | // System.out.println(result); 34 | System.out.println(reverse(s)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Question 2 | Q1.Print the word with odd letters as 3 | ``` 4 | Eg.) Input : PROGRAM 5 | Output : 6 | P M 7 | R A 8 | O R 9 | G 10 | O R 11 | R A 12 | P M 13 | ``` 14 | 15 | Q2.Alternate sorting: Given an array of integers, rearrange the array in such a way that the first element is first maximum and second element is first minimum. 16 | ``` 17 | Eg.) Input : {1, 2, 3, 4, 5, 6, 7} 18 | Output : {7, 1, 6, 2, 5, 3, 4} 19 | ``` 20 | 21 | Q3.Write function to find multipication of 2 numbers using + operator You must use minimu possible iterations. 22 | ``` 23 | Input: 3 , 4 24 | Output 12 25 | ``` 26 | 27 | Q4. Given a 9×9 sudoku we have to evaluate it for its correctness. We have to check both the sub matrix correctness and the whole sudoku correctness. 28 | ``` 29 | 5 3 4 6 7 8 9 1 2 30 | 6 7 2 1 9 5 3 4 8 31 | 1 9 8 3 4 2 5 6 7 32 | 8 5 9 7 6 1 4 2 3 33 | 4 2 6 8 5 3 7 9 1 34 | 7 1 3 9 2 4 8 5 6 35 | 9 6 1 5 3 7 2 8 4 36 | 2 8 7 4 1 9 6 3 5 37 | 3 4 5 2 8 6 1 7 9 38 | 39 | 40 | 3 1 6 5 7 8 4 9 2 41 | 5 2 9 1 3 4 7 6 8 42 | 4 8 7 6 2 9 5 3 1 43 | 2 6 3 4 1 5 9 8 7 44 | 9 7 4 8 6 3 1 2 5 45 | 8 5 1 7 9 2 6 4 3 46 | 1 3 8 9 4 7 2 5 6 47 | 6 9 2 3 5 1 8 7 4 48 | 7 4 5 2 8 6 3 1 9 49 | ``` 50 | 51 | Q5.Given sorted array check if two numbers sum in it is a given value 52 | ``` 53 | Input 54 | Array = {1 3 4 8 10 } N = 7 55 | OUTPUT: TRUE 56 | ``` 57 | 58 | Q6.Given an array of positive integers. The output should be the number of occurrences of each number. 59 | ``` 60 | Input: {2 3 2 6 1 6 2} 61 | Output: 62 | 1 – 1 63 | 2 – 3 64 | 3 – 1 65 | 6 – 2 66 | ``` 67 | 68 | Q7.Given an odd length word which should be printed from the middle of the word. 69 | ``` 70 | Example: 71 | Input: PROGRAM 72 | The output should be in the following pattern: 73 | G 74 | GR 75 | GRA 76 | GRAM 77 | GRAMP 78 | GRAMPR 79 | GRAMPRO 80 | ``` 81 | 82 | Q8.You’re given an array. Print the elements of the array which are greater than its previous elements in the array. 83 | ``` 84 | Input : 2 -3 -4 5 9 7 8 85 | Output: 5 9 8 86 | You should solve this question in O(n) time. 87 | ``` 88 | 89 | Q9. You’re given a number n. 90 | If write all the numbers from 1 to n in a paper, we have to find the number of characters written on the paper. 91 | For example if n=13, the output should be 17 if n = 101, the output should be 195 92 | 93 | Q10.Write a program to print the below pattern 94 | ``` 95 | Input: n = 6 96 | Output: 97 | 1 7 12 16 19 21 98 | 2 8 13 17 20 99 | 3 9 14 18 100 | 4 10 15 101 | 5 11 102 | 6 103 | ``` 104 | 105 | Q11. Given a set of numbers like <10, 36, 54,89,12> we want to find sum of weights based on the following conditions 106 | 1. 5 if a perfect square 107 | 2. 4 if multiple of 4 and divisible by 6 108 | 3. 3 if even number 109 | And sort the numbers based on the weight and print it as follows 110 | <10,its_weight>,<36,its weight><89,its weight> 111 | Should display the numbers based on increasing order. 112 | 113 | Q12. 114 | ``` 115 | Input: 116 | Number of elements in set1: 4 117 | Elements are: 9, 9, 9, 9 118 | Number of elements in set 2: 3 119 | Elements are: 1,1,1 120 | Output: 121 | 1, 0, 1, 1, 0 122 | 123 | Input: 124 | Number of elements in set1: 11 125 | Elements are: 7,2,3,4,5,3,1,2,7,2,8 126 | Number of elements in set 2: 3 127 | Elements are: 1,2,3 128 | Output: 7,2,3,4,5,3,1,2,8,5,1 129 | ``` 130 | 131 | Q13.Spiral printing. 132 | ``` 133 | Input : 4 134 | Output: 135 | 4444444 136 | 4333334 137 | 4322234 138 | 4321234 139 | 4322234 140 | 4333334 141 | 4444444 142 | ``` 143 | 144 | Q14.Print the numbers which are mismatched from two array. 145 | ``` 146 | Arr1 = {a b c d e f g h i} 147 | arr2 ={ a b d e e g g i i}, 148 | O/P- cd, de, fg, hi 149 | ``` 150 | 151 | Q15.Given an array and a threshold value find the o/p 152 | ``` 153 | eg) i/p {5 8 10 13 6 2};threshold = 3; 154 | o/p count = 17 155 | explanation: 156 | Number parts counts 157 | 5 {3,2} 2 158 | 8 {3,3,2} 3 159 | 10 {3,3,3,1} 4 160 | 13 {3,3,3,3,1} 5 161 | 6 {3,3} 2 162 | 2 {2} 1 163 | ``` 164 | 165 | Q16.Given two numbers a and b both < 200 we have to find the square numbers which lie between a and b(inclusive) 166 | ``` 167 | eg) i/p a = 20;b = 100; 168 | o/p 25,36,49,64,81,100 169 | ``` 170 | 171 | Q17.Given two sorted arrays, merge them such that the elements are not repeated 172 | ``` 173 | Eg 1: Input: 174 | Array 1: 2,4,5,6,7,9,10,13 175 | Array 2: 2,3,4,5,6,7,8,9,11,15 176 | Output: 177 | Merged array: 2,3,4,5,6,7,8,9,10,11,13,15 178 | 8 179 | 10 180 | 2 4 5 6 7 9 10 13 181 | 2 3 4 5 6 7 8 9 11 15 182 | 2 3 4 5 6 7 8 9 10 11 13 15 183 | ``` 184 | 185 | Q18.Rotate nXn Matrix 90 degree,180 degree, 270 degree, 360 degree. 186 | 187 | Q19.Find if the matrix is available after the rotation. 188 | 189 | ----------------------------------------------- 190 | 191 | Q20. Save the string “WELCOMETOZOHOCORPORATION” in a two dimensional array and search for substring like “too” in the two dimensional string both from left to right and from top to bottom. 192 | ``` 193 | w e L C O 194 | M E T O Z 195 | O H O C O 196 | R P O R A 197 | T I O n 198 | ``` 199 | And print the start and ending index as 200 | Start index : <1,2> 201 | End index: <3, 2> 202 | 203 | Q21. Remove unbalanced parentheses in a given expression. 204 | ``` 205 | Eg.) Input : ((abc)((de)) 206 | Output : ((abc)(de)) 207 | 208 | Input : (((ab) 209 | Output : (ab) 210 | ``` 211 | Q22. Form a number system with only 3 and 4. Find the nth number of the number system. 212 | Eg.) The numbers are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343, 3344, 3433, 3434, 3443, 3444 …. 213 | 214 | Q23. Check whether a given mathematical expression is valid. 215 | ``` 216 | Input : (a+b)(a*b) 217 | Output : Valid 218 | 219 | Input : (ab)(ab+) 220 | Output : Invalid 221 | 222 | Input : ((a+b) 223 | Output : Invalid 224 | ``` 225 | 226 | Q24. Given bigger NxN matrix and a smaller MxM matrix print TRUE if the smaller matrix can be found in the bigger matrix else print FALSE 227 | ``` 228 | Input: 229 | 3 230 | 4 5 9 231 | 1 3 5 232 | 8 2 4 233 | 2 234 | 3 5 235 | 2 4 236 | Output : True 237 | 238 | Input: 239 | 3 240 | 4 5 9 241 | 1 3 5 242 | 8 2 4 243 | 2 244 | 4 5 245 | 1 4 246 | Output : False 247 | 248 | Input: 249 | 11 12 13 14 250 | 21 22 23 24 251 | 31 32 33 34 252 | 41 42 43 44 253 | 3 254 | 21 22 23 255 | 31 32 33 256 | 41 42 43 257 | Output : True 258 | ``` 259 | 260 | Q25. Given an array, print all duplicate values. If no duplicates found print -1. 261 | ``` 262 | Input:12,13,14,12,125,24,24,12 263 | Output:12,24 264 | Input:7,9,1,21 265 | Output:-1 266 | Input:32,6,12,45,12 267 | Output:12 268 | ``` 269 | 270 | Q26. Given a linkedlist, reverse and print the list. The program should run with O(n) time and space complexity 271 | ``` 272 | Input:10 -> 20 -> 30 -> 40 -> NULL 273 | Output:40 -> 30 -> 20 -> 10 -> NULL 274 | ``` 275 | 276 | Q27: Given a string, reverse only the characters in it, any numbers present should hold the same position 277 | ``` 278 | Input:qowocorp123 279 | Output:procowoq123 280 | Input:qowo123corp 281 | Output:proc123owoq 282 | Input:qow123ocorp 283 | Output:pro123cowoq 284 | Input:a1b2cd3 285 | Output:d1c2ba3 286 | ``` 287 | 288 | Q28. Write a program to give the following output for the given input 289 | ``` 290 | Eg 1: Input: a1b10 291 | Output: abbbbbbbbbb 292 | Eg: 2: Input: b3c6d15 293 | Output: bbbccccccddddddddddddddd 294 | The number varies from 1 to 99. 295 | ``` 296 | 297 | Q29. For any given matrix find the path from the start to the end which gives the maximum sum. Traverse only right or down. 298 | Example: starting index is 15 (left top) and ending index is 10 (bottom right) 299 | ``` 300 | 15 25 30 301 | 45 25 60 302 | 70 75 10 303 | O/P:15->45->70->75->10 sum is 215 304 | ``` 305 | 306 | Q30. You’re given an even number n. If n=4, you have to print the following pattern : 307 | ``` 308 | 4444 309 | 4334 310 | 4334 311 | 4444 312 | If n=6, then the pattern should be like this : 313 | 666666 314 | 655556 315 | 654456 316 | 654456 317 | 655556 318 | 666666 319 | ``` 320 | 321 | Q31.Help john to find new friends in social network 322 | ``` 323 | Input:3 324 | mani 3 ram raj guna 325 | ram 3 kumar Kishore guna 326 | mughil 3 praveen Naveen Ramesh 327 | Output: 328 | Raj guna kumar Kishore praveen Naveen Ramesh 329 | ``` 330 | 331 | Q32.Find the union intersection of two list and also find except (remove even elements from list1 and odd elements from list2) 332 | ``` 333 | Input :7 334 | 1 3 4 5 6 8 9 335 | 5 336 | 1 5 8 9 2 337 | List 1: 1,3,4,5,6,8,9 338 | List 2: 1,5,8,9,2 339 | 340 | Union: 1,3,4,5,6,8,9,2 341 | Intersection: 1,5,8,9 342 | Except: 1,3,5,9,8,2 343 | ``` 344 | 345 | Q33. Given unsorted array find all combination of the element for a given sum. Order should be maintained. 346 | ``` 347 | Input : 8 3 4 7 9 N=7 348 | Output : {3 4 } {7} 349 | ``` 350 | 351 | Q34. Given an array, find the minimum of all the greater numbers for each element in the 352 | array. 353 | ``` 354 | Sample: 355 | Array : {2 3 7 ­1 8 5 11} 356 | 357 | Output: 358 | {2­>3, 3­>5, 7­>8, ­1­>2, 8­>11, 5­>7, 11­>} 359 | ``` 360 | 361 | Q35.Print the given pattern: 362 | ``` 363 | Input: 364 | N= 3, M=3 365 | Output: 366 | X X X 367 | X 0 X 368 | X X X 369 | N=4 M=5 370 | Output: 371 | X X X X 372 | X 0 0 X 373 | X 0 0 X 374 | X 0 0 X 375 | X X X X 376 | Input: 377 | N=6 M=7 378 | X X X X X X 379 | X 0 0 0 0 X 380 | X 0 X X 0 X 381 | X 0 X X 0 X 382 | X 0 X X 0 X 383 | X 0 0 0 0 X 384 | X X X X X X 385 | ``` 386 | 387 | Q36.Using Recursion reverse the string such as 388 | ``` 389 | Eg 1: Input: one two three 390 | Output: three two one 391 | Eg 2: Input: I love india 392 | Output: india love I 393 | ``` 394 | 395 | Q37. Given a few pairs of names in the order child, father. The input is a person name and level number. 396 | The output should be the number of children or grand children in that particular level for the person given. 397 | Example: 398 | ``` 399 | Input: 400 | [ 401 | {Ram, Syam}, 402 | {Akil, Syam}, 403 | {Nikil, Ram}, 404 | {Subhash, Ram}, 405 | {Karthik, Akil} 406 | ]; 407 | Syam 2 408 | Output: 3 (Syam has Ram and Akil in level 1 and in level 2 he have Nikil, Subhash, Karthik. So the answer is 3). 409 | ``` 410 | ``` 411 | Input: 412 | [ 413 | {Lava, kusha}, 414 | {Rama, Lava}, 415 | {Lava, Ravanan}, 416 | {Abi, Lava}, 417 | ]; 418 | I/P: Ravanan 419 | O/P: 2 420 | ``` 421 | ``` 422 | Input: 423 | [ 424 | {luke, shaw}, 425 | {wayne, rooney}, 426 | {rooney, ronaldo}, 427 | {shaw, rooney}, 428 | ]; 429 | I/P: ronaldo 430 | O/P: 2 431 | ``` 432 | 433 | Q38.Find if a String2 is substring of String1. If it is, return the index of the first occurrence. else return -1. 434 | ``` 435 | Eg 1:Input: 436 | String 1: test123string 437 | String 2: 123 438 | Output: 4 439 | Eg 2: Input: 440 | String 1: testing12 441 | String 2: 1234 442 | Output: -1 443 | ``` 444 | 445 | Q39.Arrange the numbers in descending order depending on the no. of factors available for each number. 446 | ``` 447 | I/P: {6,8,9} 448 | O/P: {8,6,9} or {6,8,9} 449 | Reason: factors of 8 (1,2,4,8), factors of 6 (1,2,3,6), factors of 9 (1,3,9). 450 | ``` 451 | 452 | -------------------------------- 453 | Q1. Given a string, replace all duplicates witht the next character. 454 | ``` 455 | java114 --> javb124 456 | BusRoute112 --> BusRovte123 457 | aBuzZ9900 --> aBuzC9012 458 | Rules: 459 | Replace uppercase characters with next uppercase character, replace lowercase with lowercase 460 | Replace z --> a, Z --> A, 9 --> 0 461 | If the next character already exists, replace the next character 462 | aBuzZ9900 -- replace Z with A 463 | As 'a' exists, replace with next character -- B 464 | As 'B' exists, replace with next character -- C 465 | aBuzC9900 --- replace 9 with 0 466 | aBuzC9000 --- replace 0 with 1 467 | aBuzC9010 --- replace 0 with 2, as 1 already exists. 468 | O/P --> aBuzC9012 469 | ``` 470 | 471 | q2.A matrix game was given with 5 rules. We were asked to implement each of the rules separately. 472 | ``` 473 | R3 | - - - | 474 | R2 | - - - | 475 | R1 | - - - | 476 | C1 C2 C3 477 | ``` 478 | Each of the 9 cells can either be empty or filled with an atom. R3, R2, R1 are the rays that originate from the left. C1, C2, C3 are the rays that originate from the bottom of the box. 479 | 480 | Input : Position of the atoms and the rays that gets originated from the outside of the box. 481 | ``` 482 | Eg.) 3 483 | 3 1 484 | 2 2 485 | 1 3 486 | 3 487 | R3 C1 C3 488 | ``` 489 | Output : Print the box. 490 | 491 | Rule 1: 492 | A ray that has an atom in its path should print ‘H’ (Hit) If it does not have any atoms in its path, the ray should pass to the other side. 493 | ``` 494 | C1 C3 495 | R3 | - - - | R3 496 | H | - X - | 497 | R1 | - - - | R1 498 | C1 H C3 499 | ``` 500 | Rule 2 & 3: 501 | A ray that has an atom in its diagonal adjacent position should refract. 502 | ``` 503 | H | - - - | 504 | H | X - - | 505 | R | - X - | 506 | R H R 507 | ``` 508 | Input rays: R1, R2, C3 509 | ``` 510 | H | - X - | 511 | R2 | - - - | C3 512 | | - - - | 513 | R2 C3 514 | ``` 515 | Rule 4: 516 | A ray that has atoms in both of the diagonal adjacent positions should reflect back. 517 | 518 | ``` 519 | Input ray: C2 520 | | - - - | 521 | | X - X | 522 | | - - - | 523 | R 524 | ``` 525 | ``` 526 | Input ray: R2 527 | | - X - | 528 | R | - - - | 529 | | - X - | 530 | ``` 531 | Rule 5: 532 | The deflection of rays should happen in the order of the input rays. 533 | ``` 534 | Input Rays: R3, R2, C1, C3 535 | H | - X - | 536 | R2 | - - - | C3 537 | | - - - | 538 | R2 C3 539 | ``` 540 | The final task was to implement these rules for dynamic matrix size. 541 | 542 | 543 | Input : no of rows, no of columns 544 | Eg.) 4 4 (row & column) 545 | 2 (No of atoms) 546 | 4 4 (Position of atom) 547 | 2 2 (Position of atom) 548 | 2 (No of rays) 549 | R4 C2 (Ray number) 550 | ``` 551 | H | - - - X | 552 | | - - - - | 553 | | - X - - | 554 | | - - - - | 555 | H 556 | ``` 557 | The final task was very confusing and it had to handle all the cases. There are chances for a ray to end at the starting position if the number of rows and columns are more than 5. 558 | 559 | Q3. Design a Call taxi booking application 560 | -There are n number of taxi’s. For simplicity, assume 4. But it should work for any number of taxi’s. 561 | -The are 6 points(A,B,C,D,E,F) 562 | -All the points are in a straight line, and each point is 15kms away from the adjacent points. 563 | -It takes 60 mins to travel from one point to another 564 | -Each taxi charges Rs.100 minimum for the first 5 kilometers and Rs.10 for the subsequent kilometers. 565 | -For simplicity, time can be entered as absolute time. Eg: 9hrs, 15hrs etc. 566 | -All taxi’s are initially stationed at A. 567 | -When a customer books a Taxi, a free taxi at that point is allocated 568 | -If no free taxi is available at that point, a free taxi at the nearest point is allocated. 569 | -If two taxi’s are free at the same point, one with lower earning is allocated 570 | -Note that the taxi only charges the customer from the pickup point to the drop point. Not the distance it travels from an adjacent point to pickup the customer. 571 | -If no taxi is free at that time, booking is rejected 572 | 573 | Design modules for 574 | ``` 575 | 1) Call taxi booking 576 | Input 1: 577 | Customer ID: 1 578 | Pickup Point: A 579 | Drop Point: B 580 | Pickup Time: 9 581 | 582 | Output 1: 583 | Taxi can be allotted. 584 | Taxi-1 is allotted 585 | ``` 586 | ``` 587 | Input 2: 588 | Customer ID: 2 589 | Pickup Point: B 590 | Drop Point: D 591 | Pickup Time: 9 592 | 593 | Output 1: 594 | Taxi can be allotted. 595 | Taxi-2 is allotted 596 | ``` 597 | (Note: Since Taxi-1 would have completed its journey when second booking is done, so Taxi-2 from nearest point A which is free is allocated) 598 | ``` 599 | Input 3: 600 | Customer ID: 3 601 | Pickup Point: B 602 | Drop Point: C 603 | Pickup Time: 12 604 | 605 | Output 1: 606 | Taxi can be allotted. 607 | Taxi-1 is allotted 608 | ``` 609 | 2) Display the Taxi details 610 | ``` 611 | Taxi No: Total Earnings: 612 | BookingID CustomerID From To PickupTime DropTime Amount 613 | ``` 614 | Output: 615 | ``` 616 | Taxi-1 Total Earnings: Rs. 400 617 | 1 1 A B 9 10 200 618 | 3 3 B C 12 13 200 619 | ``` 620 | ``` 621 | Taxi-2 Total Earnings: Rs. 350 622 | 2 2 B D 9 11 350 623 | ``` 624 | These were just sample inputs. It should work for any input that they give. 625 | Those who finished both the modules within 3 hours and if it worked for all the inputs they give, those candidates were given extra modules to work with. 626 | 627 | -------------------------------------------------------------------------------- /Train.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Train { 5 | 6 | private static int pnr = 1; 7 | private static int[][] seat = new int[5][6]; 8 | private static int[] pnrNum = new int[100]; 9 | private static int[] source = new int[100]; 10 | private static int[] destination = new int[100]; 11 | private static String[] name = new String[100]; 12 | private static int[] noOfSeats = new int[100]; 13 | private static int[][] cnfrmBooked = new int[100][6]; 14 | // private static int[][] waitingBooked = new int[100][6]; 15 | // private static int[][] seatWaiting1 = new int[5][6]; 16 | // private static int[][] seatWaiting2 = new int[5][6]; 17 | 18 | public static void doBook(){ 19 | Scanner sc = new Scanner(System.in); 20 | System.out.println("doBook"); 21 | System.out.println("Pls enter 0-4 for source station"); 22 | int src = sc.nextInt(); 23 | System.out.println("Pls enter 1-5 for source station"); 24 | int dest = sc.nextInt(); 25 | if(src==dest) { 26 | System.out.println("Source and destination is same"); 27 | System.out.println("Pls enter valid Source and destination."); 28 | doBook(); 29 | } 30 | System.out.println("Pls enter no of seats"); 31 | int n = sc.nextInt(); 32 | 33 | if(check(n,src,dest)>0) { 34 | System.out.println("Seats are not available"); 35 | } 36 | else { 37 | System.out.println("Seats are available"); 38 | 39 | System.out.println("Pls enter Ur name"); 40 | String tempName = sc.next(); 41 | 42 | int numOfStation = dest-src; 43 | int count; 44 | pnrNum[pnr]=pnr; 45 | source[pnr]= src; 46 | destination[pnr]=dest; 47 | name[pnr]=tempName; 48 | noOfSeats[pnr]=n; 49 | int temp = 0; 50 | for(int z=0;z0) { 235 | String[] strCnfrmBooked = new String[noOfSeats[i]]; 236 | 237 | for(int j =0;j