In Part 2, I have discussed 3 coding problems out of 6. Here we will talk about the next 3 coding problems.
Problem 4:
Write a function to print the following:
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
This is a rather straightforward problem. The solution is uploaded in my GitHub. One loop prints from 1 to 10 and the second loop prints from 9 to 1.
Problem 5:
Write a function to print the duplicate elements.
The solution is uploaded here. It takes an array as argument and prints the elements that occur twice in the argument array.
First I save the length of the array in a variable using the length attribute of the array. This attribute comes in handy in many programming problems.
Then I use a 2-step nested loop. The outer loop traverses through the first n-1 elements and the inner loop start from i+1 (the next element of the one indicated by the outer loop) and loops till the end of the array.
Here for each element pointed by a[j], it's compared with the element indicated by the outer loop a[i]. If they are equal, a duplicate has been detected and the element printed.
Take note that if the outer loop ends at the nth element instead of (n-1)th element, there'll be index out of bound error.
Problem 6:
Write a function to read integer values from a file and then print their sum.
I have uploaded a sample txt file for testing the function along with my solution.
Lines 9, 10 and 11 of the solution open a txt file named sum.txt. Note I have used a class named File to access the txt file.
In the while loop, a line is read using the Scanner class's nextLine() method. hasNextLine() checks if there is any line left to read from the file. The nextLine() returns the integer value as a string. Integer.parseInt() converts that string to an integer. The integer value is then added to the sum variable.
After reading and summing all the numbers from the file, the sum variable is returned.
Points to note:
So these were the 6 coding problems. In the next part, we'll discuss the 2 'error detection in code' problems. Don't forget to share and follow!!!
Note: The series starts from Part 1.
Problem 4:
Write a function to print the following:
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
This is a rather straightforward problem. The solution is uploaded in my GitHub. One loop prints from 1 to 10 and the second loop prints from 9 to 1.
Problem 5:
Write a function to print the duplicate elements.
The solution is uploaded here. It takes an array as argument and prints the elements that occur twice in the argument array.
First I save the length of the array in a variable using the length attribute of the array. This attribute comes in handy in many programming problems.
Then I use a 2-step nested loop. The outer loop traverses through the first n-1 elements and the inner loop start from i+1 (the next element of the one indicated by the outer loop) and loops till the end of the array.
Here for each element pointed by a[j], it's compared with the element indicated by the outer loop a[i]. If they are equal, a duplicate has been detected and the element printed.
Take note that if the outer loop ends at the nth element instead of (n-1)th element, there'll be index out of bound error.
Problem 6:
Write a function to read integer values from a file and then print their sum.
I have uploaded a sample txt file for testing the function along with my solution.
Lines 9, 10 and 11 of the solution open a txt file named sum.txt. Note I have used a class named File to access the txt file.
In the while loop, a line is read using the Scanner class's nextLine() method. hasNextLine() checks if there is any line left to read from the file. The nextLine() returns the integer value as a string. Integer.parseInt() converts that string to an integer. The integer value is then added to the sum variable.
After reading and summing all the numbers from the file, the sum variable is returned.
Points to note:
- sum variable is initialized to 0. If you forget to initialize it with 0, it may return garbage value.
- You need to keep in mind how txt files are accessed, read and written to. Though this may not be needed in programming contests, you may be asked to use file access codes in interviews like this one.
- Conversion from integer to string and vice versa is used in many programming problems. They are also needed interview questions, as shown here.
So these were the 6 coding problems. In the next part, we'll discuss the 2 'error detection in code' problems. Don't forget to share and follow!!!
Note: The series starts from Part 1.