Given an arithmetic equation of positive integers, +, -, * and / (no parentheses) compute the result
2*3 + 5/6*3+15 Output: 23.5
0 Comments
Write a code to remove duplicates from an unsorted linked list
Solution from Krunal
Runs in O(n), the set lookup is constant time because set implementation in Python is hashtable class Node(object): def __init__(self, data=None, next_node=None): self.data = data self.next_node = next_node def get_data(self): return self.data def get_next(self): return self.next_node def set_next(self, new_next): self.next_node = new_next @staticmethod def print_list(start_at): if start_at != None: print(start_at.data) Node.print_list(start_at.next_node) @staticmethod def remove_duplicates(head): hashSet = set() hashSet.add(head.data) prev = head current = head.next_node while (current): if current.data in hashSet: prev.next_node = current.next_node current = current.next_node else: hashSet.add(current.data) prev = current current = current.next_node head = Node(1) walker = head for i in [1,2,1,3,4,3,3,3,4,5,6,7,8,9,9]: walker.next_node = Node(i) walker = walker.next_node Node.print_list(head) Node.remove_duplicates(head) print("after removing duplicates") Node.print_list(head)
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0
a = [ [2,3,4,5,6], [2,3,0,5,6], [2,3,4,5,6], [2,3,4,5,6], [2,3,4,5,0] ] print("BEFORE") for p in a: print(p) zero_cols=[] zero_rows=[] for i in range(len(a)): for j in range(len(a[0])): if(a[i][j] == 0): zero_cols.append(j) zero_rows.append(i) for i in range(len(a)): for j in range(len(a[0])): if(i in zero_rows): a[i][j] = 0 if(j in zero_cols): a[i][j] = 0 print("AFTER") for f in a: print(f)
A child is running a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a function to count how many possible ways the child can run up the stairs
def possibleWays(steps, possibilities): if(steps == 0): return 1; if(steps < 0): return 0; return possibleWays(steps-1, possibilities)+possibleWays(steps-2, possibilities)+possibleWays(steps-3, possibilities) print(possibleWays(3, 0))
Given an NxN matrix, write a function that would rotate it by 90 degrees clock-wise or counter clockwise
So, given something like this: [2, 3, 4, 5] [2, 3, 4, 5] [2, 3, 4, 5] [2, 3, 4, 5] we would end up with something like (counter clock-wise): [5, 5, 5, 5] [4, 4, 4, 4] [3, 3, 3, 3] [2, 2, 2, 2] or (clock-wise): [2, 2, 2, 2] [3, 3, 3, 3] [4, 4, 4, 4] [5, 5, 5, 5] a = [ [2,3,4,5,6], [2,3,4,5,6], [2,3,4,5,6], [2,3,4,5,6], [2,3,4,5,6] ] print("BEFORE") for p in a: print(p) for i in range(len(a)): for j in range(len(a)-i): a[i][j], a[len(a)-j-1][len(a)-i-1] = a[len(a)-j-1][len(a)-i-1],a[i][j] print("AFTER") for f in a: print(f)
output:
BEFORE [2, 3, 4, 5, 6] [2, 3, 4, 5, 6] [2, 3, 4, 5, 6] [2, 3, 4, 5, 6] [2, 3, 4, 5, 6] AFTER [6, 6, 6, 6, 6] [5, 5, 5, 5, 5] [4, 4, 4, 4, 4] [3, 3, 3, 3, 3] [2, 2, 2, 2, 2] |
This section is for interview problems and solutions, also the interview tips for my Python students
Categories |