How to test a pen

Problem How would you test a pen? Solution This problem is largely about understand the constraints: what exactly is the pen? You should ask a lot of questions to understand what exactly you are trying to test To illustrate the technique in this problem, let us guide you through a mock-conversation. Interviewer: How would you test a pen? Candidate : What kind of pen is it? Is it a ball pen, pilot pen, roller pen, sketch pen? [Read More]

Load test a webpage without using any test tools

Problem How would you load test a webpage without using any test tools?  Solution Load testing helps to identify a web application’s maximum operating capacity, as well as any bottlenecks that may interfere with its performance. Similarly, it can check how an application responds to variations in load. To perform load testing, we must first identify the performance-critical scenarios and the metrics which fulfill our performance objectives. Typical criteria include: [Read More]

How to test a canMoveTo(int x, int y) method in a chess game

Problem We have the following method used in a chess game: boolean canMoveTo(int x, int y) x and y are the coordinates of the chess board and it returns whether or not the piece can move to that position. Explain how you would test this method.  Solution There are two primary types of testing we should do: Validation of input/output: We should validate both the input and output to make sure that each are valid. [Read More]

Find the mistake(s) in the code

Problem Find the mistake(s) in the following code: unsigned int i; for ( i = 100; i <= 0; --i) printf("%d\\n", i); Solution Because i is an unsigned integer, i cannot be negative. Hence the for loop should be: for ( i = 100; i >= 0; --i) As i is unsigned integer, when i == 0 and one does –i, i will be FFFF FFFF in hex. Then when you do printf with “%d”, i will be interpret as an signed integer. [Read More]

Programming errors cause program crashes in different places every single time

Problem You are given the source to an application which crashes when it is run. After running it ten times in a debugger, you find it never crashes in the same place. The application is single threaded, and uses only the C standard library. What programming errors could be causing this crash? How would you test each one? Solution The question largely depends on the type of application being diagnosed. [Read More]

Find a line which passes the most number of points

Problem Given a two dimensional graph with points on it, find a line which passes the most number of points. Solution Method 1 - Naive solution This is the brute force solution. We take point 1, and then point 2 and make a line. Now in the third nested loop, check if point 3 is existing on the same line or not. Pseudocode Line findLineWithMaxPoint(Set points){ foreach Point p1 in points foreach Point p2 in (points - {p1}){ Line l = makeLine(p1,p2); int count = 2 //line contains p1 and p2 foreach(Point p3 in (points-{p1,p2})){ if(l. [Read More]

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]