Sunday, April 1, 2012

GIven a 2D NxN matrix, visualize it as concentric circles. You have to find the rotated matrix where each element in the circle is rotated by 1 position layer by layer in an alternate clockwise and anticlockwise direction.


For Example 
Input:
4
2 3 4 5
1 6 7 8
4 2 1 9
5 3 2 4

Output:
1 2 3 4 
4 7 1 5 
5 6 2 8 
3 2 4 9 

Monday, February 27, 2012

Write an algorithm that prints an unordered sets of k elements chosen from a set of size n.

Example, let the size of the give set be n and set = {0, 1, 2, 3, 4} and we need to find all the subsets of size 3, then there will be a total of 10 such subsets given as:

{0, 1, 2}
{0, 1, 3}
{0, 1, 4}
{0, 2, 3}
{0, 2, 4}
{0, 3, 4}
{1, 2, 3}
{1, 2, 4}
{1, 3, 4}
{2, 3, 4}


Sunday, February 26, 2012

Fastest Super Computer is From Japan Till Today :) ,its K Computer

 Japanese supercomputer becomes world’s fastest and first to clear 10 petaflops
The fastest got faster, as the K Computer topped its own record on the twice-yearly supercomputer speed test by scoring 10.51 quadrillion calculations per second
The performance of the fastest machine on the Top500 supercomputer list hasn't stopped surging since the list began in 1993. Here, it's measured with a linear scale. 
The performance of the fastest machine on the Top500 supercomputer list hasn't stopped surging since the list began in 1993. Here, it's measured with a linear scale.
(Credit: chart by Stephen Shankland; data from Top500.org)
The rankings of the 10 fastest machines didn't change at all on today's new version of the Top500 supercomputer list, but the top dog cleared the notable performance hurdle of 10 petaflops.
"Flops" stands for floating-point operations per second and is a measure of how fast a supercomputer can perform mathematical calculations using the Linpack benchmark. The K Computer, at the Riken Advanced Institute for Computational Science in Japan, moved up from 8.16 petaflops, the score it used to reach the top of the twice-yearly supercomputer ranking last June, to 10.51 petaflops.
It reached the new top speed through being fully assembled, with all 705,024 of its Fujitsu Sparc64 processor cores running.
The Tianhe-1A in China, a former No. 1 machine at 2.57 petaflops, remains No. 2. It uses a combination of Intel central processing units and Nvidia graphical processing units. The United States is the top market for supercomputers on the list, but China is in second place, list organizers said.
Intel remains king of the heap when it comes to processor designs, supplying chips for 384 of the systems.
Among other aspects of the November 2011 list:
• Even though the top 10 machines remained the same--a first since the Top500 list began in 1993--there was still plenty of flux elsewhere on the list. The bottom of the barrel for the November list ranked No. 305 in June, and the total performance of all 500 machines rose from 58.7 petaflops to 74.2 petaflops.
• It's now required 50.9 teraflops to get on the list at all. That score beats the top system from 2004. The scores, though somewhat simplify the broad range of computing work that researchers strive to accomplish on these massive systems.
• A total of 39 systems now us graphics chips as accelerators for some computing tasks. That's the approach employed by a forthcoming Cray-built system with AMD 16-core processors and Nvidia Tesla chips slated for shipment to the National Center for Supercomputing Applications.
• IBM supplied 223 of the 500 systems and Hewlett-Packard supplied 146.
• At least 29 of the systems gulp down more than 1 megawatt of power--enough to power 10,000 100-watt light bulbs. The K Computer consumes 12.66 megawatts, but it's "one of the most efficient systems on the list," with a performance of 830 megaflops per watt, the list organizers said.
The Top500 supercomputer list shows plenty of changes over the 
years, but one constant is surging performance. This chart at the 
top shows performance in calculations per second, with a logarithmic 
scale that makes the rate of change look steady--for example by 
making the jump from 1 teraflops to 10 teraflops look as significant 
as the jump from 10 teraflops to 100 teraflops.


(Credit: Top500.org)

Thursday, February 23, 2012

Complete the function getFirstLetterWord,

Complete the function getFirstLetterWord, which takes in a string text,consisting of only lowercase letters and spaces. A word is a maximal sequence of consecutive letters. There may be multiple spaces between words. Also, text may contain only spaces.
The function getFirstLetterWord is expected to return the word which is formed by concatenating the first letter from each word in text, in the order they appear.Return an empty string is no word is possible

Sample Test Cases:

Input #00:
the quick brown fox jumped over the lazy dog

Output #00:
tqbfjotld

Explanation:
Picking up the 1st character from each of the words in the sentence, we get, tqbfjotld


Input #01:
dice     gambling     las vegas

Output #01:
dglv

Explanation:
Ignore multiple spaces, picking the 1st character from dice, gambling, las and vegas results in dglv




import java.io.FileReader;
 
class WordCount 
{
 
public static void main(String args[]) throws Exception 
{
 
String text="dice       gambling       las    vegas";//"the quick brown fox jumped over the lazy dog";
 
char ch[]=new char[text.length()];
char c;
boolean lastWhite = true;
String whiteSpace = " \t";
int i=0;
 
   for(i=0;i<text.length();i++)
   {
        c=text.charAt(i);
 
        int index = whiteSpace.indexOf(c);
        if (index == -1)
        {
                  if (lastWhite == true)
                  {
                      ch[i]=c;
                  }
        
          lastWhite = false;
 
        }
 
        else
 
        {
 
                lastWhite = true;
 
        }
 
  }
 
    String ss = new String(ch);
    System.out.println(ss);
 
 }
 
}
Run Here http://ideone.com/zVquq 

WAP to Count Tottal number of Characters, Words and Lines in File Efficiently

Guys Think its Funny & Can be solved Easily..but when interviewer ask how u counts no of words when u have more then 1 spaces or tabs,line feeds or new lines , i mean white spaces ?? Its Asked Recently from one of the my friend in Amazon & Adobe Both

here is the solution i have taken car of such Scenario

import java.io.FileReader;

class WordCount {
public static void main(String args[]) throws Exception {
int words = 0;
int lines = 0;
int chars = 0;

FileReader fr = new FileReader("E:/ubuntu/tests.txt");
int c = 0;
boolean lastWhite = true;
String whiteSpace = " \t\n\r";
char sp=' ';
char tab=' ';
char lin='\n';
char ret='\r';

System.out.println(" " +(int)sp + " " + (int)tab + " " + (int)lin + " " + (int)ret);

while ((c = fr.read()) != -1)
{
chars++;

System.out.println(c);

if (c == '\n')
{
lines++;
}

int index = whiteSpace.indexOf(c);
//icrement word count when 1st time index of any spaces occurs -1 so set next
//using boolena value lastwhite time it to false

if (index == -1)
{
if (lastWhite == true)
{
++words;
}
lastWhite = false;
}
else
{
lastWhite = true;
}

System.out.println("chars" + chars + " words" + words + " lines " + lines);
}

/*if (chars != 0)
{
++lines;
}*/


}
}

WAP to Print a Binary tree in level order with a new line after every level.

Data Structure Used Queue

Algorithm Shown Below

printLevelorder(tree)
Initialize the global variable size=0
1) Create an empty queue q
2) temp_node = root /*start from root*/ & initialize the local variable cur_size=0
3) Loop while temp_node is not NULL
A) print temp_node->data.
B) Enqueue temp_node’s children (first left then right children) to q
C) After this check cur_size is equals to zero or not
if(cur_size==0)set cur_size=size of queue(no of elements at that time in queue
D) Dequeue a node from q and assign it’s value to temp_node
E) Decrement the cur_size until it become zero so that we can re-initialize it
again with new size of queue.


#include
#include
#define MAX_Q_SIZE 100

/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};

/* frunction prototypes */
struct node** createQueue(int *, int *);
void enQueue(struct node **, int *, struct node *);
struct node *deQueue(struct node **, int *);

int size=0;

/* Given a binary tree, print its nodes in level order
using array for implementing queue */
void printLevelOrder(struct node* root)
{
int rear, front;
struct node **queue = createQueue(&front, &rear);
struct node *temp_node = root;
int nsize=0;

while(temp_node)
{
printf("%d ", temp_node->data);

/*Enqueue left child */
if(temp_node->left)
enQueue(queue, &rear, temp_node->left);

/*Enqueue right child */
if(temp_node->right)
enQueue(queue, &rear, temp_node->right);
if(nsize==0)
{
nsize=size;
printf( " \n ");
}

/*Dequeue node and make it temp_node*/
temp_node = deQueue(queue, &front);
nsize--;
}
}

/*UTILITY FUNCTIONS*/
struct node** createQueue(int *front, int *rear)
{
struct node **queue =
(struct node **)malloc(sizeof(struct node*)*MAX_Q_SIZE);

*front = *rear = 0;
return queue;
}

void enQueue(struct node **queue, int *rear, struct node *new_node)
{
queue[*rear] = new_node;
(*rear)++;
size++;
}

struct node *deQueue(struct node **queue, int *front)
{
(*front)++;
size--;
return queue[*front - 1];
}

/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

/* Driver program to test above functions*/
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->left->left = newNode(41);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->right->right = newNode(71);


printf("Level Order traversal of binary tree is \n");
printLevelOrder(root);

getchar();
return 0;
}

Time Complexity O(n)
Space Complexity O(1)
Run Here
https://ideone.com/FLgSI

Write a method to shuffle a deck of cards. It must be a perfect shuffle - in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect.


Thursday, February 16, 2012

Design The Algorithm for AutoComplete Feature of Google

Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called 'BERowser'. He happens to be working on the autocomplete function in the address line at this very moment. A list consisting of n last visited by the user pages and the inputted part s are known. Your task is to complete s to make it an address of one of the pages from the list. You have to find the lexicographically smallest address having a prefix s


Input text- 
next

select output from 
nextelement       ---- Answer
nextpermutation






Wednesday, February 15, 2012

Device An Algorithm to Find minimal number of moves the to pegs to transform from the initial to final configuration. .

There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs, output the moves required to transform from the initial to final configuration. You are required to do the transformations in minimal number of moves.
  • A move consists of picking the topmost disc of any one of the pegs and placing it on top of anyother peg.
  • At anypoint of time, the decreasing radius property of all the pegs must be maintained.

Tuesday, February 14, 2012

Convert Given Message Sequence in to Corrsponding Digits

The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. We would like to make it easier to write a message to your friend using a sequence of keypresses to indicate the desired characters. The letters are mapped onto the digits as shown below. To insert the character B for instance, the program would press 22. In order to insert two characters in sequence from the same key, the user must pause before pressing the key a second time. The space character ' ' should be printed to indicate a pause. For example, 2 2 indicates AA whereas 22 indicates B.



Example

hi      Case #1: 44 444
yes     Case #2: 999337777
foo bar Case #3: 333666 6660 022 2777
hello world   Case #4: 4433555 555666


Follow Up-Now Think if Question asked reverse e.g.
Given a telephone number print or produce all the possible telephone words or combinations of letters that can represent the given telephone number, then can you device an algorothm that will solve this problem efficiently. This is More Simple Then Previous One.