Interview Question


Country: United States




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

'''
Created on 09-Jan-2018

@author: Soma Naresh
'''
# Problem Statement
'''
A smart-set is a set of distinct numbers in which all the elements have the same number of 1s in their binary form. The set of all smallest elements from each smart-set 
that can be formed from a given array of distinct positive numbers is known as the smartest-set.

So given an array of distinct numbers, outline the elements of the smartest-set in ascending sorted order.

Example
Let the array be {6 , 2 , 11 , 1 , 9 , 14 , 13 , 4 , 18}. 
In binary form the set is {110, 010, 1011, 0001, 1001, 1110, 1101, 0100, 10010}. 
The smart-sets are {1, 2, 4}, {6, 9, 18}, {11, 13, 14}.

The smartest-set is {1,6,11} as each element is the smallest element from each smart-set.

Input Format

The first line of input consists of an integer t. This is the number of test cases. For each test case, 
the first line of input contains an integer n. Here n is the number of elements in the array. The next line contains n space separated distinct integers which are the elements 
of the array.

Output Format

The output will space separated integer elements of the smartest-set in ascending order.

Constraints

0 < t < 1000 (This is the number of test cases 
2 < n < 10000 (This is the number of integer elements of the array) 
1 < Xi < 100000 (This is the size of each element of the array)

Sample input :
3
9
6 2 11 1 9 14 13 4 18 
3
7 3 1
3
1 2 4
Sample Output :
1 6 11
1 3 7
1
'''
t = int(input())
lst = []
if 0<t<1000:
    for i in range(0,t):
        n = int(input())
        if 2<n<10000:
            temp = input().strip().split(' ')
            for k in range(len(temp)):
                temp[k] = int(temp[k])
            lst.append(list(set(temp)))
dic = {}
if lst:
    rng = len(lst)
    seq = 0
    for m in lst:
        dic1 = {}
        for n in m:
            count = bin(n).count('1')
            temp_lst = []
            if not count in dic1.keys():
                temp_lst.append(n)
                dic1[count] = temp_lst
            else:
                temp_lst = dic1[count]
                temp_lst.append(n)
                dic1[count] = sorted(temp_lst)
        dic[seq] = dic1
        seq = seq + 1
        
#print("Dictionary values")
#for each in dic.keys():
#    print(dic[each])

#print("Final Result")
for k in dic.keys():
    temp_lst2 = []
    for k1 in sorted(dic[k].keys()):
        temp_lst2.append(dic[k][k1][0])
    for a in sorted(temp_lst2):
        print(a, end = ' ')
    print('')

- SOMA NARESH January 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# Problem Statement
'''
A smart-set is a set of distinct numbers in which all the elements have the same number of 1s in their binary form. The set of all smallest elements from each smart-set 
that can be formed from a given array of distinct positive numbers is known as the smartest-set.

So given an array of distinct numbers, outline the elements of the smartest-set in ascending sorted order.

Example
Let the array be {6 , 2 , 11 , 1 , 9 , 14 , 13 , 4 , 18}. 
In binary form the set is {110, 010, 1011, 0001, 1001, 1110, 1101, 0100, 10010}. 
The smart-sets are {1, 2, 4}, {6, 9, 18}, {11, 13, 14}.

The smartest-set is {1,6,11} as each element is the smallest element from each smart-set.

Input Format

The first line of input consists of an integer t. This is the number of test cases. For each test case, 
the first line of input contains an integer n. Here n is the number of elements in the array. The next line contains n-space separated distinct integers which are the elements of the
 array.

Output Format

The output will space separated integer elements of the smartest-set in ascending order.

Constraints

0 < t < 1000 (This is the number of test cases 
2 < n < 10000 (This is the number of integer elements of the array) 
1 < Xi < 100000 (This is the size of each element of the array)

Sample input :
3
9
6 2 11 1 9 14 13 4 18 
3
7 3 1
3
1 2 4
Sample Output :
1 6 11
1 3 7
1
'''
t = int(input())
lst = []
if 0<t<1000:
    for i in range(0,t):
        n = int(input())
        if 2<n<10000:
            temp = input().strip().split(' ')
            for k in range(len(temp)):
                temp[k] = int(temp[k])
            lst.append(list(set(temp)))
dic = {}
if lst:
    rng = len(lst)
    seq = 0
    for m in lst:
        dic1 = {}
        for n in m:
            count = bin(n).count('1')
            temp_lst = []
            if not count in dic1.keys():
                temp_lst.append(n)
                dic1[count] = temp_lst
            else:
                temp_lst = dic1[count]
                temp_lst.append(n)
                dic1[count] = sorted(temp_lst)
        dic[seq] = dic1
        seq = seq + 1
        
#print("Dictionary values")
#for each in dic.keys():
#    print(dic[each])

#print("Final Result")
for k in dic.keys():
    temp_lst2 = []
    for k1 in sorted(dic[k].keys()):
        temp_lst2.append(dic[k][k1][0])
    for a in sorted(temp_lst2):
        print(a, end = ' ')
    print('')

- SOMA NARESH January 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# Problem Statement
'''
A smart-set is a set of distinct numbers in which all the elements have the same number of 1s in their binary form. The set of all smallest elements from each smart-set 
that can be formed from a given array of distinct positive numbers is known as the smartest-set.

So given an array of distinct numbers, outline the elements of the smartest-set in ascending sorted order.

Example
Let the array be {6 , 2 , 11 , 1 , 9 , 14 , 13 , 4 , 18}. 
In binary form the set is {110, 010, 1011, 0001, 1001, 1110, 1101, 0100, 10010}. 
The smart-sets are {1, 2, 4}, {6, 9, 18}, {11, 13, 14}.

The smartest-set is {1,6,11} as each element is the smallest element from each smart-set.

Input Format

The first line of input consists of an integer t. This is the number of test cases. For each test case, 
the first line of input contains an integer n. Here n is the number of elements in the array. The next line contains n-space separated distinct integers which are the elements of the
 array.

Output Format

The output will space separated integer elements of the smartest-set in ascending order.

Constraints

0 < t < 1000 (This is the number of test cases 
2 < n < 10000 (This is the number of integer elements of the array) 
1 < Xi < 100000 (This is the size of each element of the array)

Sample input :
3
9
6 2 11 1 9 14 13 4 18 
3
7 3 1
3
1 2 4
Sample Output :
1 6 11
1 3 7
1
'''
t = int(input())
lst = []
if 0<t<1000:
    for i in range(0,t):
        n = int(input())
        if 2<n<10000:
            temp = input().strip().split(' ')
            for k in range(len(temp)):
                temp[k] = int(temp[k])
            lst.append(list(set(temp)))
dic = {}
if lst:
    rng = len(lst)
    seq = 0
    for m in lst:
        dic1 = {}
        for n in m:
            count = bin(n).count('1')
            temp_lst = []
            if not count in dic1.keys():
                temp_lst.append(n)
                dic1[count] = temp_lst
            else:
                temp_lst = dic1[count]
                temp_lst.append(n)
                dic1[count] = sorted(temp_lst)
        dic[seq] = dic1
        seq = seq + 1
        
#print("Dictionary values")
#for each in dic.keys():
#    print(dic[each])

#print("Final Result")
for k in dic.keys():
    temp_lst2 = []
    for k1 in sorted(dic[k].keys()):
        temp_lst2.append(dic[k][k1][0])
    for a in sorted(temp_lst2):
        print(a, end = ' ')
    print('')

- SOMA NARESH January 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Readable code below:

for _ in range(0, int(input())):
    count = {}
    fin = []
    input()
         
    for i in input().split():
        count[int(i)] = bin(int(i)).count('1')
        
    for x in set(count.values()):
        fin.append(min([num for num, cnt in count.items() if cnt == x]))

    print(*sorted(fin), sep= ' ')

- sourabhtk37 January 11, 2018 | 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