Tag Archives: interview

Some time ago I was interviewed as python developer to appropriate position in Cogniance company. There were several test tasks:

# 1. get dict keys in descending order by values
from operator import itemgetter
d = {‘aa’: 12, ‘bb’: 32, ‘cc’: -45, ‘dd’: 43}
print [key for key, value in sorted(d.items(), key=itemgetter(1), reverse=True)]
# or
items = d.items()
items.sort(key=itemgetter(1), reverse=True) 

# 2. function to calculate sum provided as string like “i1+i2+…+in”
def calculate(str):
return reduce(lambda sum, x: sum + int(x), str.split(‘+’), 0)

“”” 3.
You are given ‘n’ strings w1, w2, ……, wn. Let Si denote the set of strings formed by considering all unique substrings of the string wi. A substring is defined as a contiguous sequence of one or more characters in the string. More information on substrings can be found here. Let ‘S’ = {S1 U S2 U …. Sn} .i.e ‘S’ is a set of strings formed by considering all the unique strings in all sets S1, S2, ….. Sn. You will be given many queries and for each query, you will be given an integer ‘k’. Your task is to output the lexicographically kth smallest string from the set ‘S’.

Input:The first line of input contains a single integer ‘n’, denoting the number of strings. Each of the next ‘n’ lines consists of a string. The string on the ith line (1<=i<=n) is denoted by wi and has a length mi. The next line consists of a single integer ‘q’, denoting the number of queries. Each of the next ‘q’ lines consists of a single integer ‘k’.

Note: The input strings consist only of lowercase english alphabets ‘a’ – ‘z’.

Output ‘q’ lines, where the ith line consists of a string which is the answer to the ith query. If the input is invalid (‘k’ > |S|), output “INVALID” (quotes for clarity) for that case.


Sample Input:

Sample Output:


For the sample test case, we have 2 strings “aab” and “aac”.
S1 = {“a”, “aa”, “aab”, “ab”, “b”} . These are the 5 unique substrings of “aab”.
S2 = {“a”, “aa”, “aac”,  “ac”, “c” } . These are the 5 unique substrings of “aac”.
Now, S = {S1 U S2} = {“a”, “aa”, “aab”, “aac”, “ab”, “ac”, “b”, “c”}. Totally, 8 unique strings are present in the set ‘S’. 
The lexicographically 3rd smallest string in ‘S’ is “aab” and the lexicographically 8th smallest string in ‘S’ is “c”. Since there are only 8 distinct substrings, the answer to the last query is “INVALID”.


import sys

def getline():
“”” interface for getting lines from some source
return sys.stdin.readline()

def substrings(str):
 “”” generator: all substrings
 n_s = len(str)
 for start in xrange(n_s):
  for end in xrange(start + 1, n_s + 1):
   yield str[start:end]

# get input data and construct S

unique_set = set()
n = int(getline())
for i in xrange(n):
for substr in substrings(getline().strip()):
S = sorted(unique_set)

# get queries and output result strings for them
for i in xrange(int(getline())):
  print S[int(getline()) – 1]
except IndexError:
  print ‘INVALID’


Yesterday, I interviewed AS3 coder position candidate. The most of times, I’m taking my part on job interview as candidate, but not yesterday.
This guy looks ok as coder. He’s provided nice sources and done with test task in half of day. But I want to talk about other thing.

Another opinion at line of hiring was about more strict test task, real one task. It would take much more time to do such real implementation. So, this test task would not look like test one. Keep in mind, the test task is just to check the skills of a candidate. On the other hand, the candidate may feel hurt by interviewer if asked for too complicated unpaid work. The company may looks like empire of evil in the eyes of stranger. Nobody wants to work in bad team or work in bad team for less money.

So, let’s being kind company with smiles that looks amazing in people eyes.