Search Byte File

Image placeholder 9899

(This question has been seen in the interviews of the following companies: Dropbox)

Find a byte array in a byte file.
For e.g.
finding arr = bytearray([3,4,5,3]) in byte file ([24,3,4,5,3,4,5,3,9, 255,...])
output: 1, 4, ...
since arr is found at idx 1, 4, ...

SOLUTION:
KMP will be one way to do it. Another way will be Rolling Hash.
Since a byte has only 256 different values, it won't be hard to create rolling hash on it. For problems like searching bit series in a binary file or searching bytes in byte file, rolling hash can be easier than KMP.
A rolling hash (also known as rolling checksum) is a hash function where the input is hashed in a window that moves through the input.

class Search:
    def __init__(self, prime):
        #the prime number controls the scale of hashcodes by (hashcode % prime)
        self.prime = prime
        
    #search for a byte array in a byte_file
    def find(self, array, byte_file):
        length = len(array)
        weight = 255 ** (length - 1) # weight of the element leaving the window
        code = self.hashcode(array)

        rolling_hash_code = self.hashcode(byte_file[:length])
        if code == rolling_hash_code:
            print "target found at [", 0, ":", length, "]"

        #start moving the window
        for i in range(length, len(byte_file)): #or read file byte after byte(if memory is the bottleneck)
            rolling_hash_code = self.rolling_hash(rolling_hash_code, byte_file[i - length], byte_file[i], weight)
            if code == rolling_hash_code:
                #then compare array with byte_file[i - length: i] to prevent false positive
                print "target found at [", i - length   1, ":", i   1, "]"
    
    #get hashcode for given byte array
    def hashcode(self, array):
        code = 0
        for byte in array:
            code = (code % self.prime * 255) % self.prime   byte
        return code % self.prime
    
    #get hashcode for bytes in window
    def rolling_hash(self, code, past, current, weight):
        code = 255 * (code - (weight % self.prime * past % self.prime) % self.prime)   current
        code %= self.prime
        return code




Get one-to-one training from Google Facebook engineers

Top-notch Professionals

Learn from Facebook and Google senior engineers interviewed 100+ candidates.
Most recent interview questions and system design topics gathered from aonecode alumnus.
One-to-one online classes. Get feedbacks from real interviewers.

Customized Private Class

Already a coding expert? - Advance straight to hard interview topics of your interest.
New to the ground? - Develop basic coding skills with your own designated mentor.
Days before interview? - Focus on most important problems in target company question bank.