Tuesday 8 September 2015

Coding interview Questions

1)      Given an integer array like - 5, 4, 3, 6, 7, 6, 5, 5

A).Find out which element is repeating max number of times and how many times it is being repeated

B).Optimize the code using exception handling for boundary conditions

Solution:

package swain.coding;

import java.util.Arrays;

public class RepeatingNumber {

       public static void main(String[] args) {
              int[] numbers = { 1, 5, 23, 23, 13, 5, 3, 7, 7, 9, 10, 12, 10, 4 };
              findRepeatingNumber(numbers);
       }

       public static void findRepeatingNumber(int[] numbers) {
              try {
                     Arrays.sort(numbers);
                     int count = 1;
                     for (int i = 1; i < numbers.length; i++) {
                           if (numbers[i] == numbers[i - 1]) {
                                  ++count;
                           } else {
                                  if (count > 1) {
                                         System.out.println("Duplicate : " + numbers[i - 1]
                                                       + " Repeating : " + count);
                                  }
                                  count = 1;
                           }
                           if ((numbers.length - 1) == i) {
                                  if (count > 1) {
                                         System.out.println("Duplicate : " + numbers[i - 1]
                                                       + " Repeating : " + count);
                                  }
                           }
                     }
              } catch (Throwable e) {
                     throw new RuntimeException();
              }
       }

}

Output:

Duplicate : 5 Repeating : 2
Duplicate : 7 Repeating : 2
Duplicate : 10 Repeating : 2

Duplicate : 23 Repeating : 2




2) Remove duplicates charecter in string ?


packageswain.javainterviewhub.blogspot.in;

public class JavaInterviewHub extends Test {

       public static void main(String[] args) {
              String str = "sitansu sekhar swain";
              System.out.println(test(str));
       }

       public static String test(String str) {
              boolean seen[] = new boolean[333];
              StringBuilder sb = new StringBuilder(seen.length);
              for (int i = 0; i < str.length(); i++) {
                     char ch = str.charAt(i);
                     if (!seen[ch]) {
                           seen[ch] = true;
                           sb.append(ch);
                     }
              }
              return sb.toString();
       }

}

Output:

sitanu ekhrw

3) WAP to find how many number of times each number appeared in the given array [3,1,6,8,12,.....] and find what is the most appeared number and should use single loop.

4) Write a algorithm to find least combination mean minimum number of combinations use for a given number suppose take 40(minimum combination is 20+20) from the given array [5,10,20,50];

5) Given a sorted array which was rotated n number of times, WAP to find the index of the smallest element and also the number of rotations using the smallest element index .Input can any  one of the following array.

1,2,3,4,5,
2,3,4,5,1,
3,4,5,1,2,
4,5,1,2,3,
5,1,2,3,4


6. Find peak value of  given series 1 3 4 5 15 8 9 1.

a) And if there is more than one peak value in series it should return the min peak value.
b) If there in no peak value it should return -1.


7. Please advice, Minimum loop required to print below Pattern (can be n*n).
4 4 4 4 4 4 4 4 3 3 3 3 3 4 4 3 2 2 2 3 4 4 3 2 1 2 3 4 4 3 2 2 2 3 4 4 3 3 3 3 3 4 4 4 4 4 4 4 4

Please check solution I used i.e. 3...


8. Second  highest and second minimum number in an  integer array.




No comments:

Post a Comment