September 2006 - Posts

using System;

System;

namespace FindMaxValue

{

class Program

FindMaxValue

{

class Program

class Program

{

static void Main(string[] args)

{

int[] myArray = new int[] {22,10,4,6,33,7,8,9,11};

Console.Write(MaxValue.FindMax(myArray));

Console.ReadLine();

}

}

class MaxValue

static void Main(string[] args)

{

int[] myArray = new int[] {22,10,4,6,33,7,8,9,11};

Console.Write(MaxValue.FindMax(myArray));

Console.ReadLine();

}

}

class MaxValue

int[] myArray = new int[] {22,10,4,6,33,7,8,9,11};

Console.Write(MaxValue.FindMax(myArray));

Console.ReadLine();

}

}

class MaxValue

Console.Write(MaxValue.FindMax(myArray));

Console.ReadLine();

}

}

class MaxValue

Console.ReadLine();

}

}

class MaxValue

class MaxValue

{

public static int FindMax(int[] array)

{

int maxValue = array[0];

for(int i=1; i<array.Length; i++)

{

if (array[i] > maxValue)

maxValue = array[i];

}

return maxValue;

}

}

}

public static int FindMax(int[] array)

{

int maxValue = array[0];

for(int i=1; i<array.Length; i++)

{

if (array[i] > maxValue)

maxValue = array[i];

}

return maxValue;

}

}

}

int maxValue = array[0];

for(int i=1; i<array.Length; i++)

{

if (array[i] > maxValue)

maxValue = array[i];

}

return maxValue;

}

}

}

for(int i=1; i<array.Length; i++)

{

if (array[i] > maxValue)

maxValue = array[i];

}

return maxValue;

}

}

}

if (array[i] > maxValue)

maxValue = array[i];

}

return maxValue;

}

}

}

return maxValue;

}

}

}

Posted by sanjeebsarangi | 4 comment(s)
Filed under:

using System;

namespace BubbleSort

{

class Program

{

static void Main(string[] args)

{

int[] array = new int[] {2,5,7,1,9,0,3,4,2,6,7,5};

// Before sorting

for (int i = 0; i < array.Length - 1; i++)

{

Console.Write(array[i]);

}

Console.ReadLine();

ArraySort.SortArray(array);

// After sorting

for (int i = 0; i < array.Length - 1; i++)

{

Console.Write(array[i]);

}

Console.ReadLine();

}

}

class ArraySort

{

public static void SortArray(int[] numbers)

{

int tempInt;

for (int i = (numbers.Length-1); i >=0; i--)

{

for (int j =1; j <=i; j++)

{

if (numbers[j-1] > numbers[j])

{

tempInt = numbers[j-1];

numbers[j-1] = numbers[j];

numbers[j] = tempInt;

}

}

}

}

}

}

Posted by sanjeebsarangi | 6 comment(s)
Filed under:

Reverse an Array is one of the most common programming algorithm questions that is asked in many tech interviews. Here is a quick way with sample code:

using System;

System;

namespace ReverseArray
{
/// <summary>
/// Reverse an Array quickly
/// </summary>

ReverseArray
{
/// <summary>
/// Reverse an Array quickly
/// </summary>

class Program
{
   static void Main(string[] args)
   {
      int[] array = new int[] {1,2,3,4,5,6,7,8,9};
      // Print before reverse
      for (int i = 0; i < array.Length; i++)
      {
         Console.Write(array[i]);
      }
      Console.ReadLine();
 

class Program
{
   static void Main(string[] args)
   {
      int[] array = new int[] {1,2,3,4,5,6,7,8,9};
      // Print before reverse
      for (int i = 0; i < array.Length; i++)
      {
         Console.Write(array[i]);
      }
      Console.ReadLine();
 

      ReverseArrayClass.ReverseArray(array);
     // Print after reverse
     for (int i = 0; i < array.Length; i++)
     {
        Console.Write(array[i]);
     }
     Console.ReadLine();
     }
}

///
<summary>
/// Array Reverse Alogorithm
/// </summary>

class ReverseArrayClass
{
   public static void ReverseArray(int[] array)
   {
      int tempInt;
      for (int i=0; i<(array.Length/2); i++)
      {
         tempInt = array[i];
         array[i] = array[array.Length -i-1];
         array[array.Length -i-1] = tempInt;
      }
   }
}
}

class ReverseArrayClass
{
   public static void ReverseArray(int[] array)
   {
      int tempInt;
      for (int i=0; i<(array.Length/2); i++)
      {
         tempInt = array[i];
         array[i] = array[array.Length -i-1];
         array[array.Length -i-1] = tempInt;
      }
   }
}
}
Posted by sanjeebsarangi | 22 comment(s)
Filed under:

I had a screening interview last week where an SDE Lead asked me what type of collection object I should be using for a given situation. Well, there was not a straight and short answer to it. But, I mentioned a few points to consider and here are they:

  • Array is the fastest among all collections available in C# unless you need sort, search and dynamically extension of array size.
     
  • ArrayList is good for storing custom object types, frequent data change, frequent insert/delete operation.
     
  • SortedList is for fast object retrieval using an index or a key. Avoid using SortedList for large data changes
     
  • Queue is for first in first out.
     
  • Stack is for last in first out
     
  • StringCollection is for storing strings.
     
  • NameValueCollection is to store strings of key-value pairs in a presorted order.This is used for data that changes frequently where you need to insert/delete items regularly and where you need to cache items for fast retrieval.
     
  • ListDictionary to store small amounts of data usually fewer than 10 items.
     
  • Hashtable is used to store a large number of records and store data that may not change frequently.
     
  • HybridDictionary is to store frequently queried data when you expect the number of records to be low with occasional increases in size.
  •  

These are the general thumb rules for choosing a collection type, but it depends on actual situation in which you need to design.

Posted by sanjeebsarangi | 54 comment(s)
Filed under:

I had to remove some dups from one of the tables in my database. Well, there is a couple of ways to do that. They are: using distinct, using derived table, correlated subqueries and using dymanic sql. Each of these technique has its own pros and cons. Well, I prefer using correlated subqueries over to others bases on the scenario that I have. It is always debatable and based on the actual situation and usage.

 Here is what I did for my table.

CREATE TABLE DuplicateRecords (

TABLE DuplicateRecords (

Id INT IDENTITY,

INT IDENTITY,

Name VARCHAR(50)

)

Name VARCHAR(50)

)

GO

INSERT DuplicateRecords (Name)

DuplicateRecords (Name)

SELECT 'Sanjeeb Sarangi' UNION ALL

SELECT 'Sanjeeb Sarangi' UNION ALL

SELECT 'Atoshi Sahoo' UNION ALL

SELECT 'Atoshi Sahoo' UNION ALL

SELECT 'Jim Boone' UNION ALL

SELECT 'Jim Boone' UNION ALL

SELECT 'Trevor Carnahan' UNION ALL

SELECT 'Trevor Carnahan' UNION ALL

SELECT 'Atoshi Sahoo' UNION ALL

SELECT 'Atoshi Sahoo' UNION ALL

SELECT 'Sanjeeb Sarangi' UNION ALL

SELECT 'Sanjeeb Sarangi' UNION ALL

SELECT 'Chad Justice' UNION ALL

SELECT 'Chad Justice' UNION ALL

SELECT 'Harpo Marx' UNION ALL

SELECT 'Harpo Marx' UNION ALL

SELECT 'Atoshi Sahoo' UNION ALL

SELECT 'Atoshi Sahoo' UNION ALL

SELECT 'Trevor Carnahan' UNION ALL

SELECT 'Trevor Carnahan' UNION ALL

SELECT 'Sanjeeb Sarangi' UNION ALL

SELECT 'Sanjeeb Sarangi' UNION ALL

SELECT 'Chad Justice' UNION ALL

SELECT 'Chad Justice' UNION ALL

SELECT 'Zeppo Marx' UNION ALL

SELECT 'Zeppo Marx' UNION ALL

SELECT 'Sanjeeb Sarangi' UNION ALL

SELECT 'Sanjeeb Sarangi' UNION ALL

SELECT 'Vinay Raturi' UNION ALL

SELECT 'Vinay Raturi' UNION ALL

SELECT 'Daniel Neely' UNION ALL

SELECT 'Daniel Neely' UNION ALL

SELECT 'Vinay Raturi'

SELECT 'Vinay Raturi'

SELECT * FROM DuplicateRecords

* FROM DuplicateRecords

DELETE DuplicateRecords

DuplicateRecords

WHERE Id > (

Id > (

SELECT MIN(Id)

SELECT MIN(Id)

FROM DuplicateRecords dups

WHERE dups.Name = DuplicateRecords.Name

FROM DuplicateRecords dups

WHERE dups.Name = DuplicateRecords.Name

WHERE dups.Name = DuplicateRecords.Name

)

SELECT * FROM DuplicateRecords

* FROM DuplicateRecords

DROP TABLE DuplicateRecords

TABLE DuplicateRecords
Posted by sanjeebsarangi | 2 comment(s)
Filed under:

http://pointerx.net/photos/screenshots/images/852/original.aspx

 

More Posts