Form a circle from 3 points

Problem Given 3 points which are not colinear (all on the same line) those three points uniquely define a circle. But, how do you find the center and radius of that circle? Propose a simple algorithm for this. Solution 1. join two pairs of vertices. 2. Form the equations for the two lines. 3. Form the equations of the perpendicular bisectors of each of the equations obtained in step 2. [Read More]

How many time function gets called?

Problem Consider the function below: void foo1() { if(A < B) Then { } else if(C < D) then foo2() } How many time foo2 () would get called given A < B 25% of the times and C < D 75% of the times and foo1 () is called 5000 times Solution This is the question of basic probability. As foo2() occurs in 2nd branch of if else, the probability of it being called is . [Read More]

Polite Numbers - Calculate all the ways such that a number can be written as sum of 2 or more consecutive numbers

Problem Write a program that calculates all the ways that a number can be written as the sum of two or more consecutive numbers and generate those sets. Background  In number theory, a polite number is a positive integer that can be written as the sum of two or more consecutive positive integers. The first few polite numbers are 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17,… [Read More]

Find average of a stream of the numbers

Problem Given a stream of numbers, print average (or mean) of the stream at every point. Example For example, let us consider the stream as 10, 20, 30, 40, 50, 60, … Average of 1 numbers is 10.00 Average of 2 numbers is 15.00 Average of 3 numbers is 20.00 Average of 4 numbers is 25.00 Average of 5 numbers is 30.00 Average of 6 numbers is 35.00 .................. Solution To print mean of a stream, we need to find out how to find average when a new number is being added to the stream. [Read More]

Print all sequences of given length

Problem Given two integers k and n, write a function that prints all the sequences of length k composed of numbers 1,2..n. You need to print these sequences in sorted order. Examples Example 1 Input: k = 2, n = 3 Output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 Example 2 Input: k = 3, n = 4 Output: 1 1 1 1 1 2 1 1 3 1 1 4 1 2 1 . [Read More]

Number of Subsets without consecutive elements

Problem Consider a set S of the first 10 natural numbers.Find the number of subsets that do not contain consecutive elements. Solution The question is based on pure calculation and some combinatorics. Subsets of length 0 = 1 Subsets of length 1 = 10 These two cases are straight forward. Subsets of length 2 = number of ways we can pick two numbers out of 10 - number of ways in which we can pick two adjacent numbers [Read More]

Match two board configurations of tic-tac-toe problem

Problem How will you match two board configurations of tic-tac-toe problem? Solution This is a quite unexpected interview question as it has nothing to do with your programming skills or algorithms. This is a plain simple question designed to check your problem solving skills. Consider the tic-tac-toe configurations : The trick in this question is that any board configuration is rotation invariant and what we mean by that is all 2 the configurations shown above are similar and must match in the logic we use for checking board configurations. [Read More]

Find next higher number using exact same digits

Problem Given a number X find the number Y which is next higher number than X using the exact same digits as in X. Example For example: given 38276 return 38627 Solution Method 1 - Brute force The brute-force method is to generate the numbers with all digit permutations and consider all the higher numbers than the given number and return the minimum number from those higher numbers. [Read More]

Convert a Number (unknown base) to a Base 10 Number

Problem Given an integer, write a program that converts the given number to a number (in base 10). The base of the given number is unknown. Solution The problem statement states that the base of the given number is unknown. Thus to proceed one must need to assume a base for the number. It is practically safe to assume that the digit with the maximum value in the number denotes the maximum that can be accounted in the unknown base. [Read More]

Euclid's Algorithm : GCD of 2 integers

The following example solves a classic elementary problem: “Reduce a given fraction to its lowest terms”, e.g. we want to write 2/3, not 4/6. We solve this problem by finding the greatest common divisor (gcd) of the numerator and the denominator by using the famous approach called Euclid’s algorithm. C Code Iterative solution // Iterative algorithm int gcd(int a, int b) { int temp; while(b) { temp = a % b; a = b; b = temp; } return(a); } Recursive solution [Read More]