Saturday, March 4, 2017

Non Recursive Pre-Order Traversal

public IEnumerator PreOrderTraversal()
        {
            if (Head != null)
            {
                Stack> stack = new Stack>();
                BinaryTreeNode current = Head;
                stack.Push(null);

                while (stack.Count!=0)
                {
                    if (current != null)
                    {
                        yield return current.Value;
                        if (current.Right != null)
                            stack.Push(current.Right);
                        if (current.Left != null)
                        {
                            current = current.Left;
                        }
                        else
                        {
                            current = stack.Pop();
                        }
                    }
                }
            }
        }

Non Recursive In-Order Traversal C#

 public IEnumerator InOrderTraversal()
        {
            BinaryTreeNode current = Head;
            Stack> stack = new Stack>();
            stack.Push(null);

            while (stack.Count != 0)
            {
                while (current != null)
                {
                    stack.Push(current);
                    current = current.Left;
                }
                current = stack.Pop();
                if (current != null)
                {
                    yield return current.Value;
                    current = current.Right;
                }
            }
        }

Monday, February 22, 2016

Does ISNULL check improve query performance

Today I came across something very interesting:


I had a query with a inner join between 2 tables and few more criteria in the where condition along with one condition like "int1

Both the tables had about 1 million records each. A simple select on both tables individually as well as joined returned all the records within 20 seconds. However when full query was executed on sql server, it continued to run for 3 hours, taking 100% CPU cycle and no results. I was wondering, why filtering should take that long.


I started adding conditions to where clause one after another and ran the query to see which where clause is taking more time. Everything worked fine. At the end I added the condition "int1

But a nagging feeling was there that something is not alright. Why should addition of an extra check improve performance. Am I missing something?


Next started the Google research "Does ISNULL check improve query performance". I landed on a link with exact same question.


The answer startled me :).


when I added the ISNULL condition, it did not use the existing execution plan. it created a new one for itself based on latest SQL server statistics.  Hence it completed fast. Execution plan for my query was based on old statistics and hence it was slow.


I update the statistics of my database by executing following:


EXEC sp_updatestats


This forced all the execution plan to get recreated again. A after this my original query ran as fast as other queries.


Conclusion: ISNULL check does not improve performance.

Monday, August 17, 2015

Hackerrank : Caeser Cipher in C#

//caeser cipher
int n = Convert.ToInt32(Console.ReadLine());

string s = Console.ReadLine();

int offset = Convert.ToInt32(Console.ReadLine());

char[] final = new char[n];

for (int i = 0; i < n; i++)



{
//bool ischar = false;

bool isSmall = false;

int curchar = (int)s[i];

if(curchar >= 97 && curchar <= 122)



{
isSmall = true;



curchar = curchar - 32;

}
if (curchar >= 65 && curchar <= 90)



{
int newChar = curchar + offset;

while(newChar >90)


{ newChar = newChar - 26; }
final[i] = isSmall ? (char)(newChar + 32) : (char)(newChar);

Console.Write(final[i]);



}
else



{

final[i] = s[i];
Console.Write(final[i]);



}

}
Console.ReadLine();

Find if 2 strings are anagram or not

/* find if 2 strings are anagram or not like silent and listen*/
string str1 = Console.ReadLine();
string str2 = Console.ReadLine();
bool isAnagram = true;
int strLen = str1.Length;
int[] arr = new int[256];
if(strLen == str2.Length)
{
for (int i = 0; i < strLen; i++)
{
arr[(int)str1[i]]++;
arr[(int)str2[i]]--;
}
foreach (int i in arr)
{
if (i > 0)
{
isAnagram = false;
break;
}
}
}
else
{
isAnagram = false;
}

Linked list: Pairwise swap

private static void SwapEveryTwoNodes(LinkedList l)
        {
            if (l == null || l.head == null)
                return;
            Node cur = l.head;
            Node next = cur.next;
            Node pre = null;
            if (cur == null || next == null)
                return;
            l.head = l.head.next;
            //in every iteration, there should be minimum 3 pointer change. Before next iteration begins,
            //ensure that the pointer to previos node is saved.           
          
            while (cur != null && next != null)
            {
                // cur =1, next = 2
                cur.next = next.next; //1.next=3
                next.next = cur; // 2.next=1
                if(pre!=null) // this check is only for first iteration
                    pre.next = next; //connect the left side of the LL with the swapped nodes
                pre = cur; //Save previous step
                cur = cur.next; //next odd numbered node
                next = cur!=null? cur.next:null; //next even numbered node              
            }
        }

Monday, April 8, 2013

Unrecognized configuration section system.web.extensions when upgrading to ASP.NET 4.0

Recently I upgraded my solution to VS 2010. My solution contained almost 30 projects. After upgrading when I ran the solution for the first time I got following error:

Unrecognized configuration section system.web.extensions when upgrading to ASP.NET 4.0

After some googling it was certain that the error was due to upgrade but I could not find exact solution. Then I opened my machine.config file (located at C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config).
It had the "system.web.extensions" section. Suddenly I noticed that the entire text in the machine.config was in small letters(system.web.extensions) whereas in my projects' we.config, the first letter of every word was in capital letters. (System.Web.Extensions). When I changed my web.config to small case, the solution worked.

One complete day wasted on such small silly thing!!!!