Problem: Court takes decision to relax the sentence given to criminal if he can solve a puzzle. Criminal has to take decision to open one of the doors. Behind each door is either a lady or a tiger. They may be both tigers, both ladies or one of each. If the criminal opens a door … Read More →
100 Wrong Statements puzzle
Puzzle: There are 100 statements. 1st one says : at least one is wrong. 2nd one says : at least two are wrong. 3rd one says : at least three are wrong. 4th one says : at least four are wrong. and so on. 100th one says : at least 100 are wrong. How many statements … Read More →
Riding Against the Wind Puzzle
Problem: A horse rider went a mile in 5 minutes with the wind and returned in 7 minutes against the wind. How fast could he ride a mile if there was no wind? Solution: Most of us will proceed like that if a rider goes a mile in 2 minutes with the wind, and returns … Read More →
Cashier’s Puzzle
Problem: An engineer goes to a bank with a check of $200 and asks the cashier “Give me some one-dollar bills, ten times as many twos and the balance in fives!”. What will the cashier do? Solution: The smallest amount of one-dollar and two-dollar bills the cashier may give to the old man is 1×1 … Read More →
Replace all spaces in a string with “%20″
Problem: Write a C program that will replace all spaces with ‘%20′ Solution: The algorithm is as follows: 1. Count the number of spaces during the first scan of the string. 2. Parse the string again from the end and for each character: If a space is encountered, store “%20”. Else, store the character as … Read More →
Find Position of the only Set Bit
Problem: You are given a number having only one “1″ its binary representation,. You have to find position of it? Solution: First of all we will check if number is power of two, Beacuse then only its binary represenattion will contain only one “1″. After that, start from rightmost bit and one by one check … Read More →
Find if a no is Power of Two
Problem: Write a C program to find if a number is of power of 2? Solution: Method 1: (Using arithmetic) Keep dividing the number by two, i.e, do n = n/2 iteratively. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes … Read More →
Counting Sort
Counting sort is a linear time sorting algorithm used to sort items when they belong to a fixed and finite set. Integers which lie in a fixed interval, say k1 to k2, are examples of such items. Algorithm: 1. Count for every key j, 1 ≤ j ≤ m how often it occurs in the … Read More →
Sort Elements By Frequency
Problem: You are given an array. You have to sort the array elements in decreasing order of their frequency. eg. [2,3,4,2,8,1,1,2] Output: [2 2 2 1 1 3 4 8] Solution: Method 1 (Use Sorting) 1) Use quick sort algorithm to sort the elements O(nlogn) 2) Scan the sorted array and construct a 2D array … Read More →
Group all Words in Set of Anagrams of Each Other in Dictionary
Problem: Given a dictionary with set of words in it. Write a program to group all words which are anagrams of each other in to sets. For example – Input: “bat”, “tar”, “xyz” , “tab”, “tar” Output:[["bat", tab"], ["tar", rat"],”xyz” ] Thi sproblem was asked in amazon interview. Steps: 1)Use some sort of a hash … Read More →
What’s going on ?