





0
Notifications Mark All Read
No New notification
- Login
- Get Prime
Automata Question – 1
Q1. You are required to fix all logical errors in the given code. You can click on Compile and
Run anytime to check the compilation/execution status of the program. You can use
System.out.printin to debug your code. The submitted code should be logically/syntactically
correct and pass all testcases. Do not write the main() function as it is not required.
Code Approach: For this question, you will need to correct the given implementation. We do
not expect you to modify the approach or incorporate any additional library methods.
The method sortArray(int arr[]) of class ArraySort accepts an integer array arr as an input and
performs an inplace sort operation on it. The function is expected to return the input array sorted
in descending order.
The function compiles successfully but fails to return the desired result due to logical errors.
Your task is to debug the program to pass all the test cases.
1 // You can print the values to stdout for debugging
2 public class ArraySort{
3 public ststic int[] sortArray(int arr[]){
4 int i, max, location, j, temp, len = arr.length;
5 for( i = 0; i<len; i++){
6 max = arr[i];
7 location = i;
8 for(j = i; j<len; j++){
9 if(max>arr[j]){
10 max = arr[j];
11 location = j;
12 }
13 }
14 temp = arr[i];
15 arr[i] = arr[location];
16 arr[location] = temp;
17 }
18 return arr;
19 }
20 }
Output: TestCase 1:
Status:
Correct
Expected:
[24, 23, 21, 14, 12]
Returned:
[24, 23, 21, 14, 12]
TestCase 2:
Ststus:
Correct
Expected:
[1, 1, 1, 1, 1]
Returned:
[1, 1, 1, 1, 1]
The program was also checked on another testcases. 6 out of 6 passed.
1 // You can print the values to stdout for debugging
2 public class ArraySort{
3 public ststic int[] sortArray(int arr[]){
4 int i, max, location, j, temp, len = arr.length;
5 for( i = 0; i<len; i++){
6 max = arr[i];
7 location = i;
8 for(j = i; j<len; j++){
9 if(max>arr[j]){
10 max = arr[j];
11 location = j;
12 }
13 }
14 temp = arr[i];
15 arr[i] = arr[location];
16 arr[location] = temp;
17 }
18 return arr;
19 }
20 }
Login/Signup to comment