Need help getting better at coding interviews
Anonymous in /c/coding_help
487
report
We are currently going through coding interviews at my university. I've encountered a few problems that I'm having trouble solving and need feedback. <br><br>1. *There are n people in a room. And n lockers. Each person will have a number written on them (1-n). Write an algorithm that determines if it is possible to assign each person to a locker based on the number on their shirt*<br><br>My attempt at this:<br>```<br>import math<br>def locker(locker_numbers,person_numbers):<br> if math.factorial(locker_numbers)>math.factorial(person_numbers):<br> return True<br> else:<br> return False <br><br>print(locker(20,19))<br># Expected output:True<br>print(locker(20,22))<br># Expected output:False<br>```<br>2. *An elevator has a weight limit of *1000 lbs. 100 people are waiting for the elevator and the weights are: 1 lbs, 2 lbs, 3 lbs, 4 lbs, 5 lbs ,\... 100 lbs. Write an algorithm that determines the optimal number of people to send in the elevator to stay under the 1000 lbs weight limit*<br><br>My attempt at this:<br>```<br>def elevator(people,weight_limit):<br> new_list=[]<br> count=0<br> for i in people:<br> new_list.append(i)<br> if sum(new_list)>=weight_limit:<br> new_list.remove(i)<br> print("Person number",i,"will not be added the the elevator")<br> new_list.append(i)<br> break<br> else:<br> count+=1<br> print("Number of people added to the elevator:",count)<br> print("Total weight:",sum(new_list))<br>elevator([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100],1000)<br># Expected output: 12, 0 to 12 added to the elevator, total weight of 78 lbs<br>elevator([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100],200)<br># Expected output: 15, 0 to 15 added to the elevator, total weight of 120 lbs<br>elevator([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100],150)<br># Expected output: 12, 0 to 12 added to the elevator, total weight of 78 lbs<br>elevator([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100],120)<br># Expected output: 11, 0 to 11 added to the elevator, total weight of 66 lbs<br>```<br>3. *Given a set of integers, output the first pair of integers that add up to the target sum.*<br><br>My attempt at this:<br>```<br>def twoSum(nums, target):<br> ans=0<br> for i in nums:<br> if ans < target:<br> ans += i<br> else:<br> ans=0<br> if ans==target:<br> print("The first pair of integers that add up to the target sum are:",i,nums[nums.index(i)-1])<br> return<br>twoSum([3,4,4,6,9,5,2,6,1,9,6,5,8,5,1,3,9,9,9,2,8,9,4,5,3,1],23)<br># Expected output: The first pair of integers that add up to the target sum are: 9 14<br>twoSum([5,3,2,6,1,8,3,1,3,9,9,2,7,2,6,6,1,8,4,9,5,4,5,8,7,4,4],11)<br># Expected output: The first pair of integers that add up to the target sum are: 4 7<br>```<br>Any help would be appreciated.
Comments (9) 14470 👁️