Microsoft Corporation Question:
Download Questions PDF

Assume you have an array that contains a number of strings (perhaps char * a[100]). Each string is a word from the dictionary. Your task is as under?

Answer:

Assume you have an array that contains a number of strings (perhaps char * a[100]). Each string is a word from the dictionary. Your task, described in high-level terms, is to devise a way to determine and display all of the anagrams within the array (two words are anagrams if they contain the same characters; for example, tales and slate are anagrams.)


Begin by sorting each element in the array in alphabetical order. So, if one element of your array was slate, it would be rearranged to form aelst (use some mechanism to know that the particular instance of aelst maps to slate). At this point, you slate and tales would be identical: aelst.
Next, sort the entire array of these modified dictionary words. Now, all of the anagrams are grouped together. Finally, step through the array and display duplicate terms, mapping the sorted letters (aelst) back to the word (slate or tales).

Download Microsoft Interview Questions And Answers PDF

Previous QuestionNext Question
write a function that will take a sorted array, possibly with duplicates, and compact the array, returning the new length of the array?You are given a scale which you are to use to measure eight balls?