Monday, July 1, 2013

Write a method to capitalize all vowels in a string (C#)

   
    static void Main(string[] args)
        {
           
            string input = "Clematis require a site that will receive at least one half day of direct sunlight. This helps in bud and bloom production, and in lowering susceptibility to crown rot.   Young clematis are extremely susceptible to crown rot, which is a disease. Crown Rot can usually be prevented by treating the plant with a garden or flower fungicide throughout the growing season.";
            Console.WriteLine (capitalizeVowels(input));
            Console.ReadKey();
            }

First Approach : Using a StringBuilder to hold the intermidiate string.

 public static string capitalizeVowels(string input)
        {
                       if (string.IsNullOrEmpty(input))
                return input;
            else
            {
                HashSet vowels = new HashSet { 'a', 'e', 'i', 'o', 'u' };
                StringBuilder result = new StringBuilder();
                foreach (var i in input)
                {
                    result.Append(vowels.Contains(i) ? char.ToUpper(i) : i);

                }
                return result.ToString();
            }
        }

Second Approach : Using Linq

  public static string capitalizeVowels(string input)
        {
            if (string.IsNullOrEmpty(input))
                return input;
            else
            {
                HashSet vowels = new HashSet { 'a', 'e', 'i', 'o', 'u' };
                return new string(input.Select(x => vowels.Contains(x) ? char.ToUpper(x) : x).ToArray());
            }
        }
 

Monday, June 24, 2013

• What does NULL mean in SQL?

In SQL, the value NULL means UNKNOWN; it does not mean '' (empty string). Assuming ANSI_NULLS are on in your SQL Server database, which they are by default, any comparison to the value NULL will yield the value NULL. You cannot compare any value with an UNKNOWN value and logically expect to get an answer. You must use the IS NULL operator instead.

Thursday, June 20, 2013

Print all possible combinations of r elements in a given array of size n (C#)

For this question, I have taken an array of size 5 and printed all combinations of length 3. I used Linq query for doing this.

Solution


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace Excercises
{
    class Program
    {
        static void Main(string[] args)
        {
            //To generate all the length-3 combinations of integers from the set {1, 2, 3, 4, 5}:
            int[] Arr = { 1, 2, 3, 4, 5 };
                var combo = from a in Arr
                            from b in Arr
                            from c in Arr
                            select string.Concat(a,b,c);
            foreach (var v in combo)
            {
            Console.WriteLine(v);
            }
            Console.ReadKey();
        }

    }
}

Tuesday, June 18, 2013

Find the least number in a given array of integers (C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
namespace examples
{
    class Program
    {
        static void Main(string[] args)
        {

//The option I chose is to sort the array (Bubble sort) and return the integer in the first index.

            int[] Arr = { 1, 10, -5, 10, 12,6,2,-9,0,4,2 };
            for (int i= 0; i < Arr.Length; i++)
            {
                for (int j = i; j < Arr.Length; j++)
                {
                    if (i == j)
                    {
                       
                    }
                    else
                    {
                        if (Arr[i] <= Arr[j])
                        {
                            //break;
                        }
                        else
                        {
                            int temp = Arr[j];
                            Arr[j] = Arr[i];
                            Arr[i] = temp;
                        }
                    }
                }
            }
                Console.WriteLine("The lowest value in the array is {0}", Arr[0]);
                Console.ReadKey();
           
        }
    }
}
 

Sunday, June 16, 2013

What will be the approach to find the word which occurs maximum number of times in a very large file. (C#)

To find a word that occurs the maximum number of times in a very large file, the entire file will have to be scanned for the occurrence of the word. Since the file is very large, it will be efficient to process the file and find the occurrence of the word asynchronously. After the file is read in a fileStream, we can have different threads to process the file in chunks simultaneously. The threads will wait till processing for the file in each thread is over. Then the word with maximum count in the file will be determined.

Friday, June 14, 2013

How do you sort an array of 100 million values, given that the values are positive and unique. Disregard the space complexity of the solution i.e. assume that you have sufficient memory.

At first glance this question sounds baffling, even though it relaxes one of the criteria of space complexity. For sorting a data store that large, the time complexity is also critical.

Here is an approach which I thought was quite ingenuous. It also takes advantage of the available space. Here goes...

Solution  


Create an array of bits whose length is the length of the maximum value of an unsigned integer. In this array, each element has a default value of 0.

So the array looks like this:

          0 , 1 ,2 , 3, 4, 5,....                                                .....N
B[] = [0][0][0][0][0][0][0][0][0][0][0][0]......[0][0][0][0][0][0]

where N is the maximum value of an unsigned integer.

Now parse the source array of numbers and for each number encountered in the source, index that into the bit array above to set that bit (i.e. change it to 1).

e.g. If S[0] = 2345 then B[2345] = 1, i.e. that corresponding index has its bit flag set to indicate that the value is in the source array.

As each of the values is considered as a lookup in the bit array, the numbers are implicitly sorted since they are indices on a contiguous memory location. The values which do not occur in the array would not have their bit flag set.

Another "side-effect" of this process is that searching for any number can be done in constant time, since it gets converted to an array lookup which is O(1). So if you want to find if a number exists in this array, all you have to do is to index the bit array and check the bit flag at that index location - if it is set then the number exists, otherwise it does not!

e.g. if you want to find if 34987 exists in the source array, check if B[34987] = 1 or 0. If it is 1 then it exists, else it does not.



Wednesday, June 12, 2013

You have a set of (x,y) corordinates - like a list of .NET Point object - that form a line when plotted on a graph. If you are given a new (x,y) co-ordinate, find if it is a point in that line. (C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
           
            List list = new List();
            Points P1 = new Points();
            P1.x = 2;
            P1.y = 4;
            Points P2 = new Points();
            P2.x = 6;
            P2.y = 1;

            Points P3 = new Points();
            P3.x = 7;
            P3.y = 4;
            list.Add(P1);
            list.Add(P2);
            list.Add(P3);
            Points P = new Points();
            P.x = 0;
            P.y = 7;
            int slope = findSlope(list);
            int newSlope = (P.y - P2.y) / (P.x - P2.x);
            Console.WriteLine((slope == newSlope).ToString());
            Console.ReadKey();
        }

//The structure of the Points object
  public  struct Points
       {
         public int x;
          public int y;
       }
public static int findSlope(List pointsList)
        {
    //    Get the 'X' and 'Y' values of any two points

        
           int x1 = pointsList.ElementAt(0).x;
            int x2 = pointsList.ElementAt(1).x;
            int y1 = pointsList.ElementAt(0).y;
            int y2 = pointsList.ElementAt(1).y;


//The slope is calculated by dividing the difference between Y co-ordinates by the difference of 'X' co-ordinates of two points

            int slope = (y2 -y1)/(x2 -x1);
            return slope;
        }
    }
}