Archive for the ‘Yahoo’ Category.

Find Lowest Common Ancestor of 2 nodes

One of the common, yet tricky interview question is to find the lowest common ancestor node for given 2 nodes in

A. Binary Tree

B. N-ary tree

Reverse Linked List

Write a program to reverse a linked list

1. With recursion

2. Without Recursion

Kth Smallest element of a Binary Search Tree

Given a Binary Search Tree, write a program to print the kth smallest element

Find the missing number

This is a very common interview questions and comes in various forms. The most common one is of the following format
Problem 1
Given an array of 999 distinct integers ranging from 1 to 1000 including. Find which number is missing. Restrictions: loop over the array only once, can’t allocate an additional array.

Another version of the problem is where 2 numbers are missing.
Problem 2
Given an array of 998 distinct integers ranging from 1 to 1000 including. Find which 2 numbers are missing.

Restrictions: loop over the array only once, can’t allocate an additional array.

Algorithm to Shuffle a Deck of Cards

Write an algorithm to shuffle an array of 52 integers so that each number has equal probability to be in any of the 52 positions.