Check if One String is a Rotation of Other String

Problem: Given two string s1 and s2 how will you check if s1 is a rotated version of s2 ? If s1 = “crazyforcode” then the following are some of its rotated versions: “forcodecrazy” “codecrazyfor” Solution: Steps: First need to check if s1 and s2 are of the same length. Then check, if s2 is Read More →

Find the Minimum Distance Between Two Numbers

Problem: You are given an array and two elements, find the minimum distance between the elements in the array. The array may have duplicates. For example, if the array is (1, 5, 3, 7, 2, 8, 3, 4, 5, 9, 9, 3, 1, 3, 2, 9) Min Distance (4, 7): 4 Min Distance (9, 3): Read More →

Find Anagrams in array of Strings

Problem: You are given an array of strings and you have to print all the anagrams within the array. What is anagram – For those who don’t know, two words are anagrams if they contain the same characters. We have already discussed how to check if 2 strings are anagrams here. Solution: Let’s use this Read More →

Find the Rotation Count in Rotated Sorted array

Problem : Find the count k by which array has been rotated in the rotated sorted array. So for example we have sorted array as 2,3,6,12, 15, 18. Now suppose the array is rotated k times ( we don’t know k), such that array becomes 15, 18,2,3,6,12 We have to find K? Solution : This Read More →

Find the Minimum Element in the Rotated Sorted array

Problem : Find the minimum element in the rotated sorted array. For example we have array as – 1,3,6,9,12,15. Now suppose the array is rotated k times ( we don’t know k), such that array becomes 12,15,1,3,6,9 Solution – The answer to this lies on the fact that if we can find the point on Read More →

Deleting all Leaves from a Binary Tree

Problem: Given a binary tree, how do you remove leaves of it? Solution: By using post-order traversal we can solve this problem (other traversals would also work). Time Complexity: O(n). Where is numbe of nodes in tree. Write or Comment if you find anything incorrect or better approach.

Move the Spaces to Front of the String

Problem: Given a string that has set of words and spaces, write a program to move the spaces to front of string, you need to traverse the array only once and need to adjust the string in place. string = “move these spaces to beginning” output =” movethesepacestobeginning” Solution: 1) Maintain two indices i and Read More →

Find a Pair Whose Sum is Closest to Zero in Array

Problem: This problem is also called minimum absolute sum pair. You are given an array of integers, containing both +ve and -ve numbers. You need to find the two elements such that their sum is closest to zero. eg. Array [15, 5, -20, 30, -45] O/P should be 15, -20 Solution: Searching for two numbers Read More →

Next Greater Element in array

Problem: You are given an array, print the next greater element for each element. Elements for which no greater element exist, print next greater element as -1. You have to reduce the number of comparisons. For any array, rightmost element always has next greater element as -1. eg. For array [4, 15, 2, 9, 20, Read More →

Mentor Graphics Interview – Set 1

Company: Mentor Graphics Technical Interview: Q1. Questions related to the work done at my previous company. Q2. Find if a given directed graph is cyclic or not. Q3. You have a stream of bytes from which you can read one byte at a time. You only have enough space to store one byte. After processing Read More →

Fab.com Interview Questions – Set 1

Company : fab.com, Pune 3 Tech rounds + 1HR Technical interview: Questions will be based on Data strutures and algorithms. Round 1: (Telephonic Interview) Q1. Counting sort algorithm? Q2. Find two numbers whose sum is closet to zero in an array? Q3. Find the no of set bits in a number. Q4. Reverse a string Read More →

Infoedge Interview – Set 1

Company : Infoedge India (Naukri.com on BSE) 1 written + 3 tech rounds + 1HR – Written will consist few puzzles, C programs of array and string, sql’s. – Tech rounds will be on Data structure, SQL, Basic Oops concepts. Focus will remain on algorithms. Whatever program they will ask to write, they will also Read More →