RSS

General Interview Questions

Sun, Jan 24, 2010

0 Comments

1. Given a integer array which contains one odd repeated number and rest are even repeated numbers. Find the odd repeated number

Example:

Input:{1,2,1} returns 2

Input:{1,1,5,6,7,5,6,7,1 } return 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 public static int FindOddrepNum(int[] a)
        {
            Dictionary tracker<int,bool> = new Dictionary<int,bool>();
            foreach (int i in a)
            {
                if (!tracker.Keys.Contains(i))
                {
                    tracker.Add(i, true);
                }
                else
                {
                    if (tracker[i])
                    {
                        tracker[i] = false;
                    }
                    else
                    {
                       tracker[i]= true;
                    }
                }
            }
            foreach (int i in a)
            {
                if (tracker[i])
                {
                    return i;
                }
            }
            return -1;
        }

2.Given an integer array and a sum find the sets of corresponding two numbers in given array with that sum

Example: Input ({1,10,15,18,4,13,19,7,12,35},25)
Output :
Sum Giving Pair are:15-10
Sum Giving Pair are:7-18
Sum Giving Pair are:12-13

public static void FindSumSets(int[] a, int sum)
        {
            List tracker<int> = new List<int>();
 
            foreach (int i in a)
            {
                tracker.Add(i);
                if (tracker.Contains((sum - i)))
                {
                    Console.WriteLine("Sum Giving Pair are:{0}-{1}", i, sum - i);
                }
            }
        }

3.Given sorted integer array and a sum find the corresponding two numbers in given array with that sum
Example: Input ({1,4,7,10,12,13,15,18,19,35},25)
output:
Sum Giving Pair are:7-18
Sum Giving Pair are:10-15
Sum Giving Pair are:12-13

public static void FindSumSetsInAsc(int[] a, int sum)
        {
            int lower = 0;
            int upper = a.Length - 1;
            int testsum = 0;
            while (lower < upper)
            {
                testsum = a[lower] + a[upper];
                if (testsum == sum)
                {
                    Console.WriteLine("Sum Giving Pair are :{0}-{1}", a[lower], a[upper]);
                    lower++;
                    upper--;
                }
                else if (testsum < sum)
                {
                    lower++;
                }
                else
                {
                    upper--;
                }
            }
        }

3. Sort Character array
can use any standard sorting algorithm but here is the one with O(n) computing complexity.

public static char[] SortcharArray(char[] a)
        {
            int[] bucket = new int[256];
            foreach (char c in a)
            {
                bucket[(int)c]++;
            }
            int k = 0;
           for(int i=0;i 0)
                {
                    a[k] = (char)i;
                    bucket[i]--;
                    k++;
                }
            }
 
            return a;
        }

4. Given a rotated sorted array and an element. Find the position of the element.
Sorted array: {-3,3,4,6,7,8,9}
Input sorted array: {7,8,9,-3,3,4,6}
Given element 3 Output 4

public static void FindEleme(int[] a,int data)
        {
            int lower = 0;
            int upper = a.Length-1;
            int mid = 0;
            int range ;
 
            while (upper >=lower)
            {
                range=upper-lower;
 
                mid = ((range) / 2) + lower;
                if (range == 0&& a[lower]!=data)
                {
                    Console.WriteLine("The Element is not in the Array");
                    return;
 
                }
 
                if (a[mid] == data)
                {
                    Console.WriteLine("The element is {0}", mid);
                    return;
                }
 
                if (a[mid + 1] > a[mid] && a[mid] > a[mid - 1])
                {
                    if (a[mid] < data)
                    {
                        lower = mid+1;
                    }
                    if (a[mid] > data)
                    {
                        upper = mid-1;
                    }                   
                }
                if (a[mid + 1] > a[mid] && a[mid] < a[mid - 1]&&a[upper]<data)
                {
                    upper = mid;
 
                }
                if (a[mid + 1] > a[mid] && a[mid] < a[mid - 1] && a[upper] >= data)
                {
                    lower = mid;
                }              
            }           
 
        }

Sorting Algorithms

Wed, Jan 20, 2010

0 Comments

Insertion Sort

If you are asked to do the Insertion sort you can follow the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 public static int[] InsertionSort(int[] a)
        {
            for (int i = 1; i < a.Length; i++)
            {
                int k = i;
                while (a[k] < a[k - 1] && k > 0)
                {
                    int temp = a[k];
                    a[k] = a[k - 1];
                    a[k - 1] = temp;
                    k--;
                }
 
            }
            return a;
        }

Selection Sort
If you are asked to write Selection Sort you can follow the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    public static int[] SelectionSort(int[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                int min = i;
                for (int j = i; j < a.Length; j++)
              {
                   if (a[min] > a[j])
                    {
                        min = j;
                    }
                }
                if (i != min)
                {
                    int temp = a[min];
                    a[min] = a[i];
                    a[i] = temp;
 
                }
            }
            return a;
 
        }

Bubble Sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static int[] BubbleSort(int[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = 0; j < a.Length; j++)
                {
                    if (a[i] < a[j])
                    {
                        int temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
            return a;        
        }

SQL Interview question

Sat, Jan 9, 2010

0 Comments

Question: Given table with schema Student(studentId,CourseId,marks) if there are 50 students and 10 courses then find the students with second highest marks in each subjects?

Example showing Extension Methods in C#

Sun, Jan 3, 2010

4 Comments

Extension Method is really a cool stuff. We can simply add the additional method to any object without knowing anything about that object.Let’s say we want a method for string object which will reverse the string then you can choose extension method to do the thing.

Binary Tree Implementation in C#

Sat, Dec 12, 2009

1 Comment

This is simple implementation of Binary Tree in C#. It includes all common methods for binary tree.

Finding Fullness quotient of a number

Sat, Dec 12, 2009

1 Comment

Define the fullness quotient of an integer n> 0 to be the number of representations of n in bases 2 through 9 that have no zeroes after the most significant digit, For example, to see why the fullness quotient of 94 is 6 examine the following table which shows the representations of 94 in bases 2 through 9.

KMP String Searching Algorithm in C#

Wed, Dec 9, 2009

1 Comment

We’ve C# example of KMP (Knuth–Morris–Pratt) string serching algorithm.

Program to reverse sentence word by word using C#

Wed, Dec 9, 2009

0 Comments

This example shows how can we reverse a sentence word by word. For example; If we have string “This is an eXclusiveMinds article” output should be “article eXclusiveMinds an is This”. For this we have two methods ReverseWordByWord and Reverse (this is helper method to reverse string character by character).
This is also one of the most frequently asked interview question in Microsoft, Google and Amazon for position of Software Design/Development Engineer (or Test).

Remove Duplicate from Array with C#

Tue, Dec 8, 2009

1 Comment

This is one of the most frequently asked interview question. This question usually asked for interview of Software Engineer/Test Engineer in Microsoft, Google, Amazon.
We’ve 3 simple solution for this question.

Simple array rotation example in C#

Tue, Dec 8, 2009

1 Comment

We’ve a solution for following frequently asked interview question. Here’s problem statement “Implement the following function, RotateArray, which takes as its input an array of unique integers that has been sorted in ascending order, then rotated by an unknown amount X where 0 <= X <= (arrayLength – 1). An array rotation by amount X moves every element array[i] to array[(i + X) % arrayLength]. RotateArray discovers and returns X by examining the array. You should be able to do this in less than linear time.”

Page 1 of 41234»