Find a line to cut two squares in half

Problem:  Given two squares on a two dimensional plane, find a line that would cut these two squares in half. Solution: Any Line passing the centers of the two squares will cut them in halves. So, we have to connect the lines between the center of the 2 squares. The straight line that connects the two centers of the two squares will cut both of them into half. [Read More]

Implement *, -, and / operators using only +

Problem Write a method to implement *, – , / operations You should use only the + operator. Solution IF we have 2 numbers a, b a * b can be implemented as adding a for b times.Take care of overflow as a*b may result in overflow. a – b can be implemented as addition of a and -b. a / b can be implemented as finding how many times you need to add b, until getting a. [Read More]

Determine whether two lines would intersect on a Cartesian plane

Problem Given two lines on a Cartesian plane, determine whether the two lines would intersect. Solution On a Cartesian plane, if two lines do not intersect, they must be parallel with each other. Hence, their slopes must be the same. If their slopes are different, they would intersect. A line is represented as ax+by+c=0 on a Cartesian plane and the slope is given by -\frac{a}{b}. Therefore if -\frac{a_{0}}{b_{0}} \neq -\frac{a_{1}}{b_{1}} for two lines, they will intersect. [Read More]

One shot or at least two shots in three games?

Problem: You have a basketball hoop and someone says that you can play 1 of 2 games. Game #1: You get one shot to make the hoop. Game #2: You get three shots and you have to make 2 of 3 shots. If p is the probability of making a particular shot, for which values of p should you pick one game or the other? Solution:  For game #1, you have probability p of winning. [Read More]

The Ant Collision Problem

Problem : There are three ants on different vertices of a equilateral triangle. What is the probability of collision (between any two or all of them) if they start walking on the sides of the triangle? Similarly find the probability of collision with ‘n’ ants on an ‘n’ vertex polygon.  Solution : Note that equilateral has nothing to do with how they will collide. Also, ants are allowed to move on the sides only. [Read More]