XOR key Discussions | Algorithms | HackerRank
  • + 0 comments

    Some people have said they overcame the timeouts for test cases 1 to 10 by switching to faster languages. I have so far tried to solve this in python 3.6, using the following approaches:

    1. Direct XOR operations
    2. Creating a trie for each query, as suggested in the Topics
    3. Accumulating tries where possible by reordering the queries, to eliminate duplicate insertions.

    Re 3, for test case 0 for example, I made an algorithm to arrange queries in this order of lower and upper indices, so that I "grow" (in this case) 3 tries by starting with the narrowest range of indices and progressively widening them

    [7,7], [5,8], [5,10], [1,13]
    [8,9], [6,10]
    [10, 14], [10,15]
    

    This way the total number of trie insertions is equivalent to the insertions needed for only the final queries of each sequence [1,13], [6,10], [10,15]. I had high hopes for the 3rd approach, but alas test cases 1 to 10 still time out and only 0, 11, 12, 13 pass, just like approaches 1 and 2.

    I can't think of any further possible refinements to the algorithm, so I am wondering if python is just too slow to overcome the timeouts. Has anyone passed cases 1 to 10 with a python solution?