Interview Question for Students


Country: United States
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 0 vote

import re
import numpy as np
with open('temp.txt','r') as f:
    s=f.readlines()[0]
x=np.array(re.findall('(.*?),\s*(.*?),\s*(.*?),\s*(\d+)\s*/?\s*', s))
idx=np.where(x[:,1]=='NULL')[0][0]
k=0
def f(x, idx, k):
    print '%s%s'%('_'*k, ' '.join(x[idx,[0,2,3]]))
    t=np.where(x[:,1]==x[idx,0])[0]
    if (len(t)>0):
        for i in t:
            f(x,i,k+1)

- DW February 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is simply a tree traversal problem. Using BFS to print out the level in the tree.
Here is a dictionary that has key as parent, and value as its children.
Each time it print out its children value. Note that the root has NULL keyword.

def treePrint():
    inp = 'Sam, Ian, technical lead, 2009 / Ian, NULL, CEO, 2007/ Fred, Sam, developer, 2010'
    nodes = inp.replace(' ','').split('/')
    tree = {}
    ceo = []
    depth = 0
    for node in nodes:
        node = node.strip().split(',')
        tree[node[1]] = [node[0]] + node[2:]
    
    while len(tree)>0:
        
        if tree.has_key('NULL'):
            ceo = (tree.pop('NULL'))
            print str(ceo).replace("'","").replace(",","").replace("[","").replace("]","") + "\n"
            depth += 1
        else:
            key = ceo[0]
            emp = tree.pop(key)
            print depth*'-' + str(emp).replace("'","").replace(",","").replace("[","").replace("]","") + "\n"
            ceo = emp
            depth +=1

- Huu P Tran April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sample input =
Sam, Ian, technical lead, 2009 / Ian, NULL, CEO,2007/ Fred, Sam, developer, 2010
Build a hash table with key as the manager and the values as their descendents
e.g.
Ian->Sam
Sam->Fred
(Since there was no boss for Ian , we didn't added it but instead recorded it as the top person in the heirarchy).
Now, start with Ian and Print Sam and again using the key Sam we got Fred.

- Klaus July 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution is using list to store employee string and then reorder it base on the supervisor attribute and name attribute. If the supervisor attribute of current index equal to the name of next index, then It's going to swap those 2 employees. Here is my code:

strq = "Sam, Ian, technical lead, 2009 / Ian, NULL, CEO,2007/Fred, Sam, developer, 2010"
def treeEmployee(infoStr):
	str1 = infoStr.split("/")
	s2 = []
	for i in str1:
		s2.append(i.split(","))
	for i in range(len(s2)):
		for j in range(1, len(s2)):
			if s2[i][1] == s2[j][0]:
				s2[i], s2[j] = s2[j], s2[i]
	return s2

- duytran211189 September 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

names = []
titles = []
y = []
for i in inp.split('/'):
names.append(i.split(', ')[0])
titles.append(i.split(', ')[2])
y.append(i.split(', ')[3])

for i in range(len(names)):
print(names[i], titles[i], y[i])

- HK July 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

import re
import numpy as np
with open('temp.txt','r') as f:
s=f.readlines()[0]
x=np.array(re.findall('(.*?),\s*(.*?),\s*(.*?),\s*(\d+)\s*/?\s*', s))
idx=np.where(x[:,1]=='NULL')[0][0]
k=0
def f(x, idx, k):
print '%s%s'%('_'*k, ' '.join(x[idx,[0,2,3]]))
t=np.where(x[:,1]==x[idx,0])[0]
if (len(t)>0):
for i in t:
f(x,i,k+1)

- DW February 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

One of the most unreadable code I have seen in a while

- Deep April 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

import csv
with open("list.csv", 'r') as csv_file:
    list_reader = csv.reader(csv_file, delimiter=",")
    employee_list = {
        name: boss.strip() for name, boss, designation, year in list_reader}
    mgrs = [k for k, v in employee_list.items() if v == '']
    while mgrs:
        print ", ".join(mgrs)
        mgrs = [k for k, v in employee_list.items() if v in mgrs]

- PierreM March 05, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More