Author Archives: Crazyadmin

Find a pair of elements from an array whose sum equals a given number

Given array of n integers and given a number X, find all the unique pairs of elements (a,b), whose summation is equal to X. Algorithm: (1) Sort the array in ascending order. Use quick sort O(n logn), we mentioned in our previous post. (2) Initialize two index variables to find the candidate elements in the Read More →

3 Bulbs and 3 Switches

3 bulbs and 3 switches puzzle

This puzzle is perhaps not as ‘pure’ as the others, it doesn’t reduce to a mathematical model. But it is quite a common question asked in interviews so it’s worth looking at it… Problem: You are in a room with 3 switches which correspond to 3 bulbs in another room and you don’t know which Read More →

Write C code to search for a value in a binary search tree (BST)

There are two ways to solve this problem. Algorithm: 1) Compere the root node if equal to data return 2) else if data < root value go to left sub tree 3) else if  data > root value go to right sub tree Method 1 : Iterative approach Method 2: Recursive approach Complexity O(log N), Read More →

Reverse a singly linked list

How to reverse a singly linked list ? This problem demonstrates the ability to work with pointers and visualize a data structure. Here we are using two pointers to reverse a list. Time complexity will be O(n) as  you have to visit every node to change the pointers.

Singly Linked List – Delete Node

You are given a pointer to a node in a singly linked list. Delete that node from the linked list. Pointer to previous node is not available. Solution: The algorithm is as the following: We have a list looking like: … -> Node(i-1) -> Node(i) -> Node(i+1) -> … and we need to delete Node(i). Read More →

25 horses and 5 lanes

25 horse and 5 lanes puzzle

Problem: There are 25 horses and 5 lanes. You have no idea about which horse is better than other. Find in minimum possible races, the first three fastest running horses. Solution: We will have 5 races with all 25 horses Let the results be u1,u2,u3,u4,u5 v1,v2,v3,v4,v5 x1,x2,x3,x4,x5 y1,y2,y3,y4,y5 z1,z2,z3,z4,z5 Work through a process of elimination: Read More →

100 Doors Puzzle

100 doors toggle puzzle

Puzzle: : You have 100 doors in a row that are all initially closed. you make 100 passes by the doors starting with the first door every time. the first time through you visit every door and toggle the door (if the door is closed, you open it, if its open, you close it). the Read More →

Interface in java

What is an Interface? An interface is a description of a set of methods that conforming implementing classes must have. An interface may never contain method definitions. One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java must have exactly one base class. A Java class may implement, and an Read More →

Boys and Girls

boys girls google interview puzzle

Question: In a country where everyone wants a boy, each family continues having babies till they have a boy. After some time, what is the proportion of boys to girls in the country? (Assuming probability of having a boy or a girl is the same) Answer: “very simple”. Half the couples have boys first, and Read More →

C program to reverse a string

For example if a user enters a string “crazyforcode” then on reversing the string will be “edocrofyzarc “. We show you two different methods to reverse string the first one we make our own function to reverse string using pointers and in second, reverse string using recursion. Method 2 : Using recursion In recursion method Read More →

Ant and Triangle Problem

Ants and triangle puzzle

Puzzle: Three ants are sitting at the three corners of an equilateral triangle. Each ant starts randomly picks a direction and starts to move along the edge of the triangle. What is the probability that none of the ants collide? Puzzle Solution: So let’s think this through. The ants can only avoid a collision if Read More →

Write a program to count nodes in a tree

Approach: Traverse all the nodes in a tree. You can do any traversal and just start counting. But there is a concept called tail recursion which is handy and very easily readable. If you have a tree, if you know the number of nodes in the left and number of nodes in right +1, then, Read More →