Wednesday, November 21, 2018

Interview Questions at Enosis(Part 3)

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:

  1. sum variable is initialized to 0. If you forget to initialize it with 0, it may return garbage value. 
  2. 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.
  3. 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.

5 comments:

  1. Thanks for this article. These questions were helpful in my interview.
    https://www.flowerbrackets.com/while-loop-in-java/

    ReplyDelete
  2. // Write a function to read integer values from a file and then print their sum.
    public class Reading
    {
    public int Sum(string path)
    {
    string[] f1 = File.ReadAllLines(path);
    var result = 0;
    for (int i = 0; i < f1.Length; i++)
    {
    var k = Convert.ToInt32(f1[i]);
    result = result + k;
    }

    return result;
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    var line = @"E:\Projects\ProblemSolving\GlassDoor5\Test.txt";
    var test = new Reading();
    Console.WriteLine(test.Sum(line));
    }
    }

    ReplyDelete
  3. Please tell about the coding test..

    ReplyDelete

Interview Questions at Enosis(Part 3)

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...