Ad

Showing posts with label Easy. Show all posts
Showing posts with label Easy. Show all posts

Tuesday, 16 July 2013

Algo#46: Determine growing direction of stack.

Note down the address of a local variable. Call another function with a local variable declared in it and check the address of that local variable and compare both.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Friday, 12 July 2013

Algo#42: Multiply by 7

Multiply given number by 7 without using * operator. What we can do is, multiply given number by 8 and then subtract given number from result. Now problem reduce to multiply by 8 rather than 7. But we know to multiply any number with power of 2, we can use shift operator. 

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Thursday, 11 July 2013

Algo#41: Free given binary tree.

Given problem of freeing data structure checks whether you know in & out of given data structure or not. You can verify memory leaks using Valgrind.

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Tuesday, 9 July 2013

Algo#39: Trim spaces from given string

Trim spaces from given string, can be asked in 2 ways. Removing all spaces from given string is easy to do than trimming only initial & end spaces.

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Monday, 8 July 2013

Algo#38: Print range of numbers without loop

To print range of numbers without using loop, we can use our usual recursion concept again.

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Saturday, 6 July 2013

Algo#36: String passing from function

Many times we need to pass string from one function to other. There are different ways to do it. Each way has some method of allocating memory either in stack or heap. We need to be aware of allocation method when we use particular way.

Following is implementation of above concept in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Friday, 5 July 2013

Algo#35: Check whether given number is power of two

To check whether given number is power of two, is problem of doing bits manipulation.

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Thursday, 4 July 2013

Algo#34: Find GCD of two numbers.

To find Greatest Common Divisor - GCD , we can use well known Euclidean algorithm.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Tuesday, 2 July 2013

Algo#32: Generate spiral matrix or helical matrix.

It seems very easy problem of just printing 2D matrix in some format. But it includes many corner cases which needs to be taken care of. Such problems can test your coding practice.

Following is implementation of above problem in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Monday, 1 July 2013

Algo#31: Find nth node in inorder traversal.

Given problem of finding nth node in in-order traversal can be easily done if we keep count of nde visited while doing in-order traversal. We will do keep this counter as static rather than global. Though taking static variable are also not encouraged in general because of parallel programming. But as of now in our case it will do.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Saturday, 29 June 2013

Algo#29: Check whether given number's bit representation is palindrome or not.

To find whether anything is palindrome or not,easiest method is to reverse it and check whether it is same as original. We will follow same approach for checking whether given number's bit representation is palindrome or not.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Sunday, 23 June 2013

Algo#23 Search given key in BST, if not present return next higher value than key which is present

Search given key in BST, if not present return next higher value than key which is present.

If we think just for a while, we will come to know that it is nothing but modified search in BST only. We have to maintain node when ever we are taking left turn in BST. Thats it. Done. Why it will work like this? Because if we are going right side, it means our key is grater than value at current node. So we don't need that value which is less than our key.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Saturday, 22 June 2013

Algo#22: Swap nibbles of number.

Nibbles are nothing but chunk of 4 bits in simple language.

To swap nibbles in given number, we have to extract 4-bits chunk from number and depending on system, we have to shift them accordingly.

Following is implementation of above algorithm in c language for 8-bit system ( Imaginary ).





Please write comments if you find anything wrong or you want to add something more related to this topic.

Tuesday, 18 June 2013

Algo#18: Add two numbers without using plus(+) operator.

There are many ways to do one thing. Given problem very well follows it. This kind of problems will test how much comfortable you are with concepts as well as language. Because both will improve your skills to solve problem in more than one way.

There is no single algorithm for such tricks. Practice makes (wo)man perfect.

Following is implementation of some of the tricks in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Monday, 17 June 2013

Algo#17: Check whether array is sorted or not using recursion

This program basically test your recursion skills. So what should be our approach when questions like this comes.

When we are solving any problem with recursion, we have to identify datum which we are going to process in one iteration of recursion. Here in our case, we will process one element of array at a time, it means remaining n-1 element should be process by recursion.

Following is simple recursive algorithm for Checking array for sorted property using recursion.
Step 1: If we are having only one element, then by default it is sorted. Success.
Step 2: Otherwise, relationship between first and second element. first element <= second element. If not then failure, else go to step 3.
Step 3: Get success or failure indicator for remaining array of n-1 element using recursion.If remaining array comes out to be sorted, then whole array is sorted.

Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Thursday, 13 June 2013

Algo#13: Cyclic right shift of bits in number

Manipulation of bits are very crucial if you can apply it properly in your computing because it is one of the fastest technique for computing.

Following is simple algorithm for finding Cyclic Right Shift of bits in given number by S bits.

Step 1: Take right S bits of number, and shift it right by R-S positions, where R is number of total bits in given number, call it Partial-1.
Step 2: Take left R-S bits of number, and shift it left by S positions, call it Partial-2.
Step 3: Do OR operation between Partial-1 and Partial-2.

Following is implementation of above algorithm in c language in one line.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Monday, 10 June 2013

Algo#10: Binary Search Technique

Binary search technique is very well known for its fast search result provided given array is sorted.

Following is simple iterative algorithm for Binary Search in Sorted Array.
Step 1: Set l(left) at start index of array.
Step 2: Set r(right) at end index of array.
Step 3: Repeat following until r>=l
        3.1: Initialize mid using current left and right index (l+(r-l)/2).
        3.2: If element at mid index is target element, then return mid index as successful binary search.
        3.3: Otherwise if element at mid index is greater than target element, then replace r = mid -1
        3.4: Otherwise if element at mid index is less than target element, then replace l = mid +1
Step 4: If you reach here, element you are searching for is not found.
Following is implementation of above algorithm in c language.





Please write comments if you find anything wrong or you want to add something more related to this topic.

Ad