Problem Given a number x, less than 100. How will you generate true with probability x/100. So if x = 65, how will you generate true with probability 65/100. You can represent true by 1 and false by 0.
Solution We can make use of rand() function to generate a number less than 100 by taking rand()%100. If the number is less than x, you can output true, otherwise you output false.
[Read More]
Given a number 123456789, two opearators + and *, value k , find all the such expressions that evaluates to the given value k
Problem Given a number 123456789 and two opearators + and *. You can use this two operators as many times u want. But you cant change the sequence of the number given there. The evaluated value is 2097.
e.g. 1+2+345*6+7+8+9=2097
You have to find all the such expressions that evaluates and value is equal to the given value. You can use concatenation of numbers like 345 is concatenated there.
[Read More]
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]
Maximum subset of Cuboid boxes that can fit into one another
Problem Given a lot of cuboid boxes with different length, breadth and height. We need to find the maximum subset which can fit into each other.
Example For example:
If Box 1 has LBH as 7 8 9
If Box 2 has LBH as 5 6 8
If Box 3 has LBH as 5 8 7
If Box 4 has LBH as 4 4 4 then answer is 1,2,4
[Read More]
REST vs RPC
REST
REST stands for Representational State Transfer.
Consider the case that you are maintaining a blog and posts on the server. Now lets understand REST.
The fundamental philosophy behind REST is that clients should communicate with servers through stateless connections, where:
Long term state is kept on the server side by maintaining a set of identifiable resources ( posts and comments in our blog case) The client can access these resources (perform CRUD operations on them) through a highly limited but uniform interface (a set of URLs in our blog case).
[Read More]