# Interview Questions Vol.1

**Python – Predictive Hacks**, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)

Want to share your content on python-bloggers? click here.

It is common to be asked to build some functions in Python (or any other programming language) during interviews in order to assess your coding style, your skills and your way of thinking. Let’s provide some examples:

Assume that we have a sequence of N rolling Die results, taking values from 1 to 6. Assuming that your input will be a sequence of results like “56611166626634416” build the following functions:

**Question 1**

How many times did it occur in the trial, that exactly two 6s were rolled after each other? For example, in sequence “**56611166626634416**” it occurred twice, that exactly two 6s were thrown after each other.

**Solution 1**

def question_1(myseq): N = len(myseq) if N<=1: return(0) if N==2 and myseq=='66': return(1) count_6s = 0 for d in range(len(myseq)-2): if d>0 and myseq[d:d+2]=='66' and myseq[d+2]!='6' and myseq[d-1]!='6': count_6s+=1 # check for the last two elements if myseq[-2:]=='66' and myseq[-3:-2]!='6': count_6s+=1 # check for the first two elements for sequences greater than 2 if len(myseq)>2 and myseq[0:2]=='66' and myseq[2]!='6': count_6s+=1 return(count_6s)

**Question 2**

Find the length of the longest subsequence of successive rolls, in which the value 6 does not occur. (This number can be zero, if only 6s were thrown.). For example, in the trial “**66423612345654**” the longest subsequence of successive rolls in which the value 6 does not occur is 12345. Its length is 5.

**Solution 2**

def question_2(myseq): # if all the elements are equal to 6 then return 0 if all(x=='6' for x in list(myseq)): return(0) # if all the elements are not equal to 6 then return the length of the sequence if all(x!='6' for x in list(myseq)): return(len(myseq)) # The successive rolls end when the 6 occurs and that is why we store these positions end_of = [] for i, d in enumerate(list(myseq)): if d=='6': end_of.append(i) # add the position of the last elemnt in case is not 6 if myseq[-1]!=6: end_of.append(len(myseq)) # then we get the first differences and finally the max lenlist = [end_of[i+1]-end_of[i]-1 for i in range(len(end_of)-1)] # add the first difference of the first element lenlist.append(end_of[0]) return(max(lenlist))

**Question 3**

We shall call a sequence of successive rolls in the trial a lucky series, if the contains only 5s and 6s. For example, “**6556665″ **is a lucky series, with a length of 7. Find out, which is the most frequent length for lucky series. If there are more than one “most frequent” lucky series lengths, print the longest. If there are no lucky series in the trial, print zero.

def question_3(myseq): # if all the elements are less than 5 then return 0 if all(int(x)<5 for x in list(myseq)): return(0) # if all the elements are greater than 4 then return the length of the sequence if all(int(x)>4 for x in list(myseq)): return(len(myseq)) # The successive rolls end when a roll is less than 5 end_of = [] for i, d in enumerate(list(myseq)): if int(d)<5: end_of.append(i) # add the position of the last elemnt in case is not 5 or 6 if int(myseq[-1])>4: end_of.append(len(myseq)) # get the length of each sequence lenlist = [end_of[i+1]-end_of[i]-1 for i in range(len(end_of)-1)] # add the first difference of the first element lenlist.append(end_of[0]) # ignore the 0 lengths lenlist = [el for el in lenlist if el!=0] # get the most frequenct length and in case of draw return the longest using the sort output = max(sorted(lenlist, reverse=True), key=lenlist.count) return(output)

**The Takeaway**

Be prepared to face these types of questions for some interview processes. Note that the above solutions are not optimized for speed and efficiency. There are indicative solutions. Feel free to provide your solutions in the comments below!

**leave a comment**for the author, please follow the link and comment on their blog:

**Python – Predictive Hacks**.

Want to share your content on python-bloggers? click here.