Skip to content

Programming

Sathiyanarayanan S edited this page Jan 12, 2021 · 5 revisions

What is the computational complexity of finding the most frequent word in a document?

Answer is N, where N is the size of vocabulary.

Assuming size of vocabulary is limited, we can have a map of size N, with key as word and value as frequency. Parse the document, add elements to the map and increment the count. Finally parse the map once to find the word with the highest frequency.


Write an algorithm to know if a String is palindrome using Linked list


Swap two integers without using a temporary variable.

Assume x1 and x2 are the variables

x1 = x1 + x2

x2 = x1 - x2

x1 = x1 - x2


Write a program to compute pairwise distance between the columns of a square matrix.

  1. What will be the dimension of the resulting matrix
  2. Give properties of the resulting matrix

100 people standing in a circle in an order 1 to 100. No. 1 has a sword. He kills the next person (i.e. No. 2) and gives the sword to the next (i.e. No. 3). All people do the same until only 1 survives. Which number survives at the last?

This is an interesting problem, Lets say N (8) people are in the circle Round 0: 1 2 3 4 5 6 7 8
Round 1: 1 3 5 7
Round 2: 1 5
Round 3: 1

When N is equal to 2^x where x is integer, the person who start with the sword survives. Initially if N is not power of 2, then we need to see when it will become power of 2. In our N 100 case, highest power of 2 less than 100 is 64. 36 people have to die for the circle to have 64 people. 36 will die in a consecutive sequence of 72 people. When this happens 73 will be holding the sword, hence he survives the game.

Simple binary tick is represent N in binary N = 100 = 1100100, Now shift the MSB to LSB 1001001 convert this representation to integer 73, here we got the solution.