Ad

Saturday 20 July 2013

Algo#50: Find k element pairs out of given array which adds to SUM

Find k element pairs out of given array which adds to SUM

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.

Friday 19 July 2013

Algo#49: Sort the input character array based on the given dictionary order.

Sort the input character array based on the given dictionary order.

E.G. If input word is “SHEEP“, sorting will make it as “EEHPS“.

But in the given dictionary, E may not be at first. As per the dictionary in given problem, if P is first, S followed and E later and finally H.

Then sorted array should be “PSEEH“.

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 18 July 2013

Algo#48: Find minimum window length which accommodate all given character.

Given problem is purely string manipulation problem. Here we have to find minimum window length which accommodate all given character for second string.

E.g. Given string ABBACBAA, and if we want to find minimum window length which can accommodate "AAA", it will be 5 (index 3 to 7). "CCC" should return MAX length as no window in original string can accommodate "CCC".

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.

Wednesday 17 July 2013

Algo#47: Word search in matrix of N*N

Word search can be solved with backtracking method.

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 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.

Monday 15 July 2013

Algo#45: Find middle node from given singly linked list using recursion.

We can find middle node from given linked list in one look of singly linked list using recursion (Ignoring stack removal access).

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.

Sunday 14 July 2013

Algo#44: Modify linked list to put nodes alternate from begin & end

To modify linked list to put nodes alternate from begin & end, we will use recursion. So we can access nodes in forward and reverse direction simultaneously. And we don't need to go back and forth to access nodes from begin and end.
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 13 July 2013

Algo#43: Write basic function to implement atoi - Alpha to Integer

Function atoi is used to convert alphabetical string to integer. We can parse given string one character at a time, and convert it to integer. To make conversion fast enough, we can use bits manipulation for faster calculation.

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.

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.

Wednesday 10 July 2013

Algo#40: Union Find Data Structure in C

Union Find Data Structure is useful to reduce time complexity in many problems e.g. Kruskal’s algorithm. 

Following is implementation of above data structure 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.

Sunday 7 July 2013

Algo#37: Calculate x^y in less time complexity than O(y)

Brute force way to calculate power of any number is to multiply given x number, power y times. But recursion can drastically reduce time complexity.

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.

Wednesday 3 July 2013

Algo#33: Using existing library functions.

This problem seems different from others. But many times in interview, you can expect this type of question of using existing library function.

Given teams score and number of times they won. We have to sort team based on these. If there is tie on score, sort them using number of times they won.

We can do this easily with qsort rather than starting from scratch.

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 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.

Ad