Given a stream of number, like 1,3,5,4,6,9 print 1,3-6,9

Revision en1, by wish_me, 2017-09-06 11:51:42

My approach->

create a hash map that contains the value of the ending index of the previous sequence.

For example

when we get 1,3,5 the hash map is

1 0

3 1

5 2

key is the number and the value is the index where it occurs.

when we get 4, we have to check for the index of 3 and 5. (One above and one below)

the value at 3 is 1 and 4’s index is 3, so they can’t be paired.

We then check for 5. 5’s index is 2 and 4’s is 3, since they are together, they can be paired.

1 0

3 1

5 2-3

4 2-3

and then we check for 3 again, 3’s index is 1 and 4’s index is 2-3, so they can match

1 0

3 1-3

5 1-3

4 1-3

We go on.

Then they alterd the question like

modified more to include distributed systems in it. And asked me if there were several systems which received the input in round robin manner, how would I implement the above algorithms.

Tags hash, array

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English wish_me 2017-09-06 11:51:42 948 Initial revision (published)