Open In App

Puzzle | Guess the bit string

Puzzle: Your friend thinks of an N bit string that will refer to it as a code. The task is to guess the code by asking your friend a series of queries. In each query, you can provide your friend with an N bit string of your choice, and your friend will tell you the number of bits in your string that coincides with the corresponding bits in the code. For example, if the code is 01011 and your query string is 11001, then the answer will be three because the two strings have the same bits on the second, third, and fifth positions. Devise an algorithm that can identify any N bit code in no more than N query.

Solution:



Example: Let N = 3 and the code which your friend thought be “110”.
The code can be guessed in 3 queries.

  1. Query 1 = “000”: The answer to the query is 1 as only the third-bit matches in the string “110” and “000”. The resultant code bit string has a total of 1 zero.
  2. Query 2 = “100”: The answer to the query is 2 as only two-bit matches in the string “100” and “110”. Since in this query changing only the first bit from the last query and the resultant code bit increased so the first bit of code string will be ‘1’.
  3. Query 3 = “110”: The answer to the query is as since three-bit matches. So now since we changed only the first bit from the last query and the answer increased so the second bit of code string will be ‘1’.

From the above, we know that two of the bits are ‘1’. And we have 1 zero in the code string so the third bit will be a ‘0’. Therefore, code string = “110”

Article Tags :