Popular Posts

Tuesday, February 7, 2012

All array questions

Saturday, February 4, 2012

All Tree questions

Construct a Binary search tree

If you are given a number N, build a binary search tree of size N with node values from 1 to N.

public class BuildTree {

public Node getTree(int n) {
if (n <= 0) {
System.out
.println("Asking for constructing a tree of size zero or negative number.");
return null;
}
return buildTree(1, n);
}

public Node buildTree(int left, int right) {
if (left > right)
return null;

int rootValue = (int) Math.floor((left + right) / 2);
Node node = new Node(rootValue);
node.left = buildTree(left, rootValue - 1);
node.right = buildTree(rootValue + 1, right);
return node;
}

public static class Node {

public int value;
// member variables will be initialized to null by default.
public Node left;
public Node right;

public Node(int value) {
this.value = value;
}

public Node(int value, Node left, Node right) {
this.value = value;
this.left = left;
this.right = right;
}
}
}

Sunday, January 29, 2012

Find the 8digit number

Find a 8-digit number, where the first figure defines the count of zeros in this number, the second figure the count of numeral 1 in this number and so on....  


Start with 8 digit number, starting from left to right, 
first digit indicate number of 0's in 8 digit number, 
second digit indicate number of 1's in 8 digit number. 
Similary 7th digit indicate number of 7's in 8 digit number.


70000000 //8 digit number 7 indicate number of 0's in the number.
70000001 //As number of 7's = 1 so 7th place is 1
60000001 //As due to 1 on 7th place number of zeros = 6
60000010 //As 6 is present so 7th place, number of 6's = 1; number of 7's = 0
61000010 //As 1 is present on 7th place ; number of 1's = 1 so 2nd place there should be 1
51000010 //As number of 0's = 5 
51000100 //As 5 is present so 6th place is 1 number of 6's = 0; number of 7's = 0 
52000100 //As number of 1's = 2  
52100100 //As 2 is present on 1st place so 3rd place is 1
42100100 // As number of  0's = 4 so 1st place is 4
42101000 // As 4 is present so 5th place is 1; number of 5's, 6's and 7's = 0;
We got the answer as 4210 1000.


Source : http://anandtechblog.blogspot.com/2011/07/find-8digit-number-microsoft-interview.html

Saturday, January 28, 2012

What really happens when you navigate to a URL

As a software developer, you certainly have a high-level picture of how web apps work and what kinds of technologies are involved: the browser, HTTP, HTML, web server, request handlers, and so on.
In this article, we will take a deeper look at the sequence of events that take place when you visit a URL.

http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/

Friday, January 27, 2012

Trim Binary Search Tree


Given the root of a binary search tree and 2 numbers min and max, trim the tree such that all the numbers in the new tree are between min and max (inclusive). The resulting tree should still be a valid binary search tree. So, if we get this tree as input:
and we’re given min value as 5 and max value as 13, then the resulting binary search tree should be:
We should remove all the nodes whose value is not between min and max. We can do this by performing a post-order traversal of the tree. We first process the left children, then right children, and finally the node itself. So we form the new tree bottom up, starting from the leaves towards the root. As a result while processing the node itself, both its left and right subtrees are valid trimmed binary search trees (may be NULL as well).

Find Next Palindrome Number

Given a number, find the next smallest palindrome larger than the number. For example if the number is 125, next smallest palindrome is 131.
Java program : 
public class NextPalindrome {
public static void main(String[] args) {
int n = 2133;

// temp variable to perform operations on n
int temp = n;
// calculate the number of digits
int digits = 0;
while (temp != 0) {
temp = temp / 10;
digits++;
}

// edge case if the given number contains all 9 s like 9 or 99 or 999
// or...,round off to next number
int divider = (int) Math.pow(10, digits);
if ((n + 1) % divider == 0) {
n = n + 1;
digits++;
}
if (digits == 0)
return;

// handling the special case of single digit numbers
if (digits == 1) {
System.out.println("Next higher palindrome is : " + n + 1);
return;
}

// create array of size digits to store the digits of the given number
int[] a = new int[digits];
temp = n;
for (int i = digits - 1; i >= 0 && temp != 0; i--) {
a[i] = temp % 10;
temp = temp / 10;
}

// now array contains the digits in same order as of number
int nextPalindrome = 0;
boolean isPalindrome = false;// used for track whether we got palindrome
// or not
while (!isPalindrome) {
// create mirror image of the given number
createMirrorImage(a);
nextPalindrome = constructNumber(a);
if (nextPalindrome <= n) {
int middle = (int) Math.ceil(digits / 2);
// if the middle digit is 9 then round off to the next higher
// number
if (digits % 2 == 0) {
if (a[digits / 2] == 9) {
a[middle] = 0;
a[middle - 1] = 0;
a[middle - 2] += 1;
} else {
a[middle] += 1;
a[middle - 1] += 1;
}
} else {
if (a[middle] == 9) {
a[middle] = 0;
a[middle - 1] = a[middle - 1] + 1;
} else {
a[middle] += 1;
}

}
} else {
isPalindrome = true;
}
}
System.out.println("Next higher palindrome is : " + nextPalindrome);
}

private static int constructNumber(int[] a) {
int num = 0;
for (int i = 0; i < a.length; i++) {
num = num * 10 + a[i];
}
return num;
}

private static void createMirrorImage(int[] a) {
int length = a.length;
for (int i = 0, j = length - 1; i <= j; i++, j--) {
a[j] = a[i];
}
}
}

Refer :  http://www.ardendertat.com/2011/12/01/programming-interview-questions-19-find-next-palindrome-number/