├── README.md ├── mock05 └── mock05.md ├── mock04 └── mock04.md ├── mock06 └── mock06.md ├── mock02 └── mock02.md ├── mock07 └── mock07.md ├── mock01 └── mock01.md └── mock03 └── mock03.md /README.md: -------------------------------------------------------------------------------- 1 | # RIP mock interviews 2 | 3 | Mock Interviews sul server discord di Mr. R.I.P. 4 | 5 | ## Mock Interviews Effettuate 6 | 7 | 1. [12/10/2022: Ranzeb vs cheeto](mock01/mock01.md) 8 | 2. [19/10/2022: Ionut_02 vs Lorenzo La Corte](mock02/mock02.md) 9 | 3. [02/11/2022: ale3683 vs Marco Zamboni](mock03/mock03.md) 10 | 4. [09/11/2022: vincenzo vs ggigazz](mock04/mock04.md) 11 | 4. [16/11/2022: MarcoZamboni vs HERZ](mock05/mock05.md) 12 | 4. [02/03/2023: MarcoZamboni vs you_33](mock06/mock06.md) 13 | 4. [08/03/2023: ale3683 vs Ranzeb](mock07/mock07.md) 14 | 5. [22/03/2023: risoluzione problemi mock 6] tbd -------------------------------------------------------------------------------- /mock05/mock05.md: -------------------------------------------------------------------------------- 1 | # Mock 05 | 16/11/2022 | MarcoZamboni vs HERZ 2 | 3 | The fibonacci sequence is defined as F(1) = 1; F(2) = 1; F(N) = F(N-1) + F(N-2) for all natural numbers N except 1 and 2. 4 | Given an index k output the k-th number of the fibonacci sequence. 5 | 6 | Input #1: 7 | k=2 8 | 9 | Output #1: 10 | 1 11 | 12 | Input #2: 13 | k=7 14 | 15 | Output #2: 16 | 13 17 | 18 | 19 | ### Links & material 20 | - [Similar LeetCode problem](https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/) 21 | 22 | ## Interview solution 23 | 24 | ```py 25 | def fibonacci1(n): 26 | if n == 1 or n == 2: # b1 27 | return 1 28 | elif n in fib_res: # b2 29 | return fib_res[n] 30 | else: # b3 31 | fib_res[n] = fibonacci1(n - 2) + fibonacci1(n-1) 32 | return fib_res[n] 33 | 34 | def fibonacci2(n): 35 | for i in range(3, n): 36 | 37 | 38 | # fibonacci(5) -> 2 + 3 = 5 39 | # fibonacci(3) -> 1 + 1 = 2 40 | # fibonacci(4) -> 2 + 1 = 3 41 | # tc = O(k) 42 | # n = dimension of input 43 | # n ~ log(k) 44 | 45 | # O(2^n) 46 | 47 | ``` 48 | 49 | ## Follow up 50 | 51 | Assume now that your solution is called Q times in sequence with an input between 1 and K, do you want to modify your code? 52 | 53 | ```py 54 | def test(): 55 | for _ in range(0, Q) # i = i..Q-1 56 | fibonacci(randint(1, K)) 57 | 58 | # O(max(Ki)) 59 | ``` 60 | 61 | Now that you’re comfortable with the Fibonacci sequence we want to decompose a natural number n 62 | into a set of Fibonacci numbers such that their sum is equal to n, 63 | return one set that satisfy that condition of -1 if such a set doesn’t exist 64 | 65 | 66 | Input #1: 67 | 8 68 | 69 | Output #1: 70 | [8] 71 | 72 | Input #2: 73 | 17 74 | 75 | Output #2: 76 | [1, 1, 2, 5, 8] -------------------------------------------------------------------------------- /mock04/mock04.md: -------------------------------------------------------------------------------- 1 | # Mock 04 | 09/11/2022 | Vincenzo vs ggigazz 2 | 3 | ## Exercise description 4 | 5 | Given the head of a linked list, determine if the linked list has a cycle in it. 6 | 7 | There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. 8 | Return 1 if there is a cycle in the linked list. Otherwise, return 0. 9 | 10 | Example1: 11 | 12 | 3 -> 2 -> 0 -> -4 13 | ^ | 14 | | | 15 | ------------ 16 | 17 | Output: 1 18 | 19 | Example2: 20 | 21 | 1 -> 2 22 | ^ | 23 | ------ 24 | 25 | Output: 1 26 | 27 | Example3 28 | 29 | 1 30 | 31 | Output: 0 32 | 33 | The number of the nodes in the list is in the range [0, 104]. 34 | -105 <= Node.val <= 105 35 | 36 | ### Links & material 37 | - Problem: https://leetcode.com/problems/linked-list-cycle/description/ 38 | - Solution: https://leetcode.com/problems/linked-list-cycle/solutions/1829489/c-easy-to-understand-2-pointer-fast-slow/?orderBy=hot 39 | - Follow up: https://leetcode.com/problems/linked-list-cycle-ii/ 40 | - Solution: https://leetcode.com/problems/linked-list-cycle-ii/solutions/1701128/c-java-python-slow-and-fast-image-explanation-beginner-friendly/?orderBy=hot 41 | 42 | 43 | ## Interview solution 44 | 45 | ```c 46 | struct ListNode { 47 | int val; 48 | struct ListNode *next; 49 | }; 50 | 51 | int checkCycle(struct ListNode *head){ 52 | int flag = 0; 53 | //outer cycle 54 | for(struct ListNode *node = head; *node != NULL; node = node->next){ 55 | struct ListNode *toBeChecked = node; 56 | //inner cycle 57 | for(struct ListNode *innerNode = node; innerNode != NULL; innerNode = innerNode->next){ 58 | //check the address, if equal break and return 1 59 | if(toBeChecked == innerNode){ 60 | //condition of termination 61 | flag = 1; 62 | break; 63 | } 64 | } 65 | if(flag == 1){ 66 | break; 67 | } 68 | } 69 | 70 | return flag; 71 | } 72 | 73 | ``` 74 | -------------------------------------------------------------------------------- /mock06/mock06.md: -------------------------------------------------------------------------------- 1 | # Mock 06 | 02/03/2023 | MarcoZamboni vs you_33 2 | 3 | You are a visitor in the capital city of Algoland and you need to attend an algorithm conference tomorrow morning. 4 | Tonight instead you'll attend a party and probably go to bed quite late, but since you really value your sleep you want to find what is the latest time you can depart from your hotel when going the the conference. 5 | We can see the city as a system of streets and interceptions, each street connects 2 interceptions, can be walked in both directions and take some time to be walked. 6 | Intersections are numbered from 0 to N-1, your hotel is located at intersection 0 and the conference venue at intersection N-1. 7 | What is the latest time t you can depart from your hotel if you want to arrive at the conference at time t_0 = 0? (Note that t will be a negative number) 8 | 9 | The input contains the number N of intersection and a list of streets, each street is defined by three numbers: a, b and t, it connects intersections a and b and takes t time to be walked. 10 | 11 | 12 | ### Example 13 | In: 14 | 15 | 5 16 | 0 1 6 17 | 0 3 1 18 | 1 2 2 19 | 1 3 2 20 | 1 4 5 21 | 2 3 1 22 | 2 4 5 23 | 24 | Out: 25 | 26 | 7 27 | 28 | Path: 29 | 30 | 0 -> 3 -> 2 -> 4 31 | 32 | 33 | ## Interview solution 34 | 35 | ```py 36 | #intersection = node 37 | #street = edge 38 | # 39 | graph = { 40 | 0 : [(1,6), (3,1)] 41 | } 42 | 43 | 44 | def Solution(N: int, streets: list[tuple[int, int, int]]): 45 | graph = defaultdict(list) 46 | for street in streets: # O(E) 47 | graph[street[0]].append((street[1],street[2])) 48 | graph[street[1]].append((street[0],street[2])) 49 | MAX_FLOAT = float("inf") 50 | distances = {node:MAX_FLOAT for node in range(0,n-1)} # O(V) 51 | distances[start]=0 52 | queue = heap([(0,0)]) 53 | 54 | while queue: 55 | (distance, node) = queue.pop() #O(1) 56 | if distance > distance[node]: 57 | continue 58 | for neighbor in graph[node]: 59 | d = neighbor[1] + distance #(1) 60 | if d < distances[neighbor[0]]: #(1) 61 | distances[neighbor[0]] = neighbor[1] + distance #(1) 62 | queue.update((d, neighbor)) #(LOG(V))) 63 | #(V+E*Log(E)) 64 | return 0-distance[N-1] 65 | 66 | 67 | ``` -------------------------------------------------------------------------------- /mock02/mock02.md: -------------------------------------------------------------------------------- 1 | # Mock 02 | 19/10/2022 | Ionut_02 vs Lorenzo La Corte 2 | 3 | ## Exercise description 4 | 5 | Given an m x n matrix and a target value, determine the location of the target value in the matrix. If the target value is not found in the matrix, print [Target Value] not found in the matrix. 6 | 7 | Every row and column is sorted in increasing order in the given matrix. 8 | 9 | ### Links & material 10 | * [Search in a row-wise and column-wise sorted matrix](https://www.educative.io/answers/how-to-search-in-a-row-wise-and-column-wise-sorted-matrix) 11 | 12 | 13 | ## Interview solution 14 | 15 | ```py 16 | def searchMatrix(matrix, num : int): 17 | row1 = matrix[0] 18 | rowIndex = len(row)-1 19 | colIndex = 1 20 | 21 | for idx, elem in enumerate(row1): 22 | if elem == num: 23 | return True 24 | elif elem > num: 25 | rowIndex = idx 26 | 27 | while(True): 28 | if num == matrix[rowIndex][colIndex]: 29 | return True 30 | else: 31 | rowIndex -= 1 32 | 33 | ``` 34 | 35 | ## Official solutions 36 | 37 | ```java 38 | import java.util.Scanner; 39 | 40 | public class Main { 41 | 42 | private static int[] search(int[][] matrix, int target) { 43 | if (matrix.length == 0) { // If there are no elements in the matrix return -1 44 | return new int[]{-1, -1}; 45 | } 46 | 47 | int numRows = matrix.length; 48 | int numCols = matrix[0].length; 49 | 50 | int i = 0; // Loop invariable for the row 51 | int j = numCols - 1; // Loop invariable for the column 52 | 53 | while (i >= 0 && i < numRows && j >= 0 && j < numCols) { 54 | if (target == matrix[i][j]) return new int[]{i, j}; 55 | else if (target < matrix[i][j]) j--; 56 | else i++; 57 | } 58 | 59 | return new int[]{-1, -1}; 60 | } 61 | 62 | public static void main(String[] args) { 63 | int[][] matrix = { 64 | {35,45,55,65}, 65 | {40,50,60,70}, 66 | {52,54,62,73}, 67 | {57,58,64,75} 68 | }; 69 | 70 | Scanner scanner = new Scanner(System.in); 71 | int target = scanner.nextInt(); 72 | 73 | int[] result = search(matrix, target); 74 | 75 | if(result[0] != -1 && result[1] != -1) { 76 | System.out.println(target + " found at row - " + (result[0] + 1) + " and column - " + (result[1] + 1)); 77 | } else { 78 | System.out.println(target + " not found in the matrix"); 79 | } 80 | } 81 | } 82 | ``` 83 | -------------------------------------------------------------------------------- /mock07/mock07.md: -------------------------------------------------------------------------------- 1 | # Mock 07 | 08/03/2023 | ale3683 vs Ranzeb 2 | 3 | Samantha is a cartographer who is on a mission to explore a remote island in the Pacific Ocean. 4 | Her task is to map the island's terrain and natural resources, and to do so, 5 | she must travel from the north-east coast to the south-west coast, crossing the island through its dense forests, deserts, rivers and hills. 6 | 7 | The island is divided into a grid of n x m cells, each of which represents a specific location on the island. 8 | Samantha has a detailed map of the island, which shows the location of every cell. 9 | She has not been given a specific route that she must follow to reach her destination, 10 | and she needs to check if there exists at least one valid path from the starting point to the ending point of the island. 11 | 12 | Your task is to create a function that takes the grid as input and determines whether there exists at least one valid path 13 | from the starting point (0,0) to the end point (n-1,m-1) and returns it. 14 | 15 | O(n*m) time, O(n*m) space 16 | 17 | input: 18 | grid = [ 19 | [0, 0, -1, 0], 20 | [0, 0, 0, -1], 21 | [-1, -1, 0, 0], 22 | [0, 0, 0, 0] 23 | ] 24 | 25 | simplest case = [ 26 | [0] 27 | ] 28 | 29 | output: (0,0) 30 | 31 | sample output [(0,0) , (1,1), etc..] 32 | 33 | 34 | ### Links & material 35 | ... 36 | 37 | ## Interview solution 38 | 39 | ```java 40 | public boolean findPath(int[][] grid, boolean[][] visited, List> finalPath, int x, int y) { 41 | 42 | if(x < 0 || x > grid.length - 1 || y < 0 || y > grid[0].length - 1) { 43 | return false; 44 | } 45 | 46 | if(grid[x][y] == -1 || visited[x][y] == true) { 47 | return false; 48 | } 49 | 50 | if(x == grid.length - 1 && y == grid[0].length - 1 && grid[x][y] != -1) { 51 | return true; 52 | } 53 | 54 | visited[x][y] = true; 55 | 56 | 57 | for(int i = 0; i < 4; i++) { 58 | List node = new ArrayList<>(); 59 | node.add(x); 60 | node.add(y); 61 | finalPath.add(node); 62 | 63 | if() 64 | if(!findPath(grid, visited, finalPath, x - 1, y)) { 65 | finalPath.remove(finalPath.size() - 1); 66 | } 67 | 68 | } 69 | 70 | 71 | /* 72 | 73 | findPath(grid, visited, finalPath, x + 1, y); 74 | findPath(grid, visited, finalPath, x, y - 1); 75 | findPath(grid, visited, finalPath, x, y + 1); 76 | */ 77 | 78 | return true; 79 | } 80 | 81 | 82 | 83 | public List> exploreIsland(int[][] grid) { 84 | List> finalPath = new ArrayList<>(); 85 | boolean[][] visited = new boolean[grid.length][grid[0].length]; 86 | 87 | findPath(grid, visited, finalPath, 0, 0); 88 | 89 | return finalPath; 90 | } 91 | 92 | ``` 93 | -------------------------------------------------------------------------------- /mock01/mock01.md: -------------------------------------------------------------------------------- 1 | # Mock 01 | 12/10/2022 | ranzo vs cheeto 2 | 3 | ## Exercise description 4 | 5 | You’re given a non-empty array of positive integers where each integer represents the maximum number of steps you can take forward in the array. 6 | 7 | For example, if the element at index 1 is 3, you can go from index 1 to index 2, 3, or 4. 8 | 9 | Write a function that returns the minimum number of jumps needed to reach the final index. 10 | 11 | Note that jumping from index i to index i + x always constitutes one jump, no matter how large x is. 12 | 13 | Sample input : array = [3, 4, 2, 1, 2, 3, 7, 1, 1, 1, 3] 14 | Sample output : 4 // 3 → ( 4 or 2 ) → ( 2 or 3 ) → 7 → 3. 15 | 16 | 17 | “”” 18 | [1, 3, 2, 5, 3, 2] 19 | res = 3 20 | “”” 21 | 22 | ### Links & material 23 | * [Jump Game II - Greedy - Leetcode 45 - Python](https://www.youtube.com/watch?v=dJ7sWiOoK7g), Neetcode solution of similar problem 24 | 25 | ## Interview solution 26 | 27 | ```py 28 | from collections import deque 29 | 30 | def min_number_of_jumps(arr: List[int]) -> int: 31 | if not arr: 32 | return 0 33 | s = set() 34 | res = 0 35 | q = deque() 36 | q.append(0) 37 | while q: 38 | res += 1 39 | size = len(q) 40 | for _ in range(size): 41 | p = q.popleft() 42 | s.add(p) 43 | for i in range(1, arr[p]+1): 44 | if p + i >= len(arr) -1: 45 | return res 46 | if p+i not in s: 47 | q.append(p+i) 48 | 49 | raise ValueError(“invalid array”) 50 | ``` 51 | 52 | [1, 3, 2, 5, 3, 2] 53 | res = 3 54 | s = {0, 1,2} 55 | q = { 3, 4} 56 | 57 | [3,10,3,2,1,...] 12 58 | [1, 1, 1, 1, 1] 59 | 60 | ## Official solutions 61 | 62 | Hint 1: 63 | 64 | Try building an array of the minimum number of jumps needed to go from index 0 to all indices. Start at index 0 and progressively build up the array, using previously calculated values to find next ones. 65 | 66 | Hint 2: 67 | 68 | Building the array mentioned in Hint #1 should be feasible using two for loops. In an effort to optimize your algorithm, realize that at any point in the array you know the farthest index that you can reach as well as number of steps that you have left until you must “consume” a jump 69 | 70 | Hint 3: 71 | 72 | After initializing your maximum reach as well as your current number of steps to the value stored at index 0, you can easily update your maximum reach as you traverse the input array by simply comparing it to the value stored at each index. You can also remove one step from your current number of steps at each index, since moving from one index to the next uses up one step. When your steps reach zero, find a way to calculate how many steps you actually have left using the maximum reach and the index that you’re at. 73 | 74 | **First solution:** 75 | 76 | 77 | ```java 78 | public static int minNumberOfJumps(int[] array) { 79 | // Write your code here. 80 | int[] jumps = new int[array.length]; 81 | Arrays.fill(jumps, Integer.MAX_VALUE); 82 | jumps[0] = 0; 83 | for(int i = 1; i < array.length; i++) { 84 | for(int j = 0; j < i; j++) { 85 | if(array[j] + j >= i) { 86 | jumps[i] = Math.min(jumps[i], jumps[j] + 1); 87 | } 88 | } 89 | } 90 | return jumps[array.length-1]; 91 | } 92 | ``` 93 | 94 | 95 | **Optimal solution:** 96 | 97 | ```java 98 | public static int minNumberOfJumps(int[] array) { 99 | // Write your code here. 100 | if(array.length == 1) return 0; 101 | 102 | int maxReach = array[0]; 103 | int steps = array[0]; 104 | int jumps = 0; 105 | 106 | for(int i = 1; i < array.length - 1; i++) { 107 | maxReach = Math.max(maxReach, array[i] + i); 108 | steps--; 109 | 110 | if(steps == 0) { 111 | jumps++; 112 | steps = maxReach - i; 113 | } 114 | } 115 | 116 | return jumps + 1; 117 | } 118 | ``` 119 | -------------------------------------------------------------------------------- /mock03/mock03.md: -------------------------------------------------------------------------------- 1 | # Mock 03 | 02/11/2022 | ale3683 vs Marco Zamboni 2 | 3 | ## Exercise description 4 | 5 | You have a graph of n nodes. 6 | You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph. 7 | Return true if there's more than one component otherwise false. 8 | There are no repeated edges. 9 | 10 | input #1: 11 | n = 5, edges = [[0,1],[1,2],[3,4]] 12 | 13 | output #1: 14 | true 15 | 16 | why? 17 | there are two components: 0-1-2 3-4 18 | 19 | input #2: 20 | n = 5, edges = [[0,1],[1,2],[2,3],[3,4]] 21 | 22 | 23 | output #2: 24 | false 25 | 26 | why? 27 | there is just one component: 0-1-2-3-4 28 | 29 | ## Follow Up 30 | - follow-up 1: If you can change the input to have already the adjs list built what that would change in time complexity? 31 | - follow-up 2: How you would count how many components we have in the graph? 32 | - follow-up 3: How you would count how many edges we need to add to have just one component? 33 | - follow-up 4: How you would findout between which nodes we need to add an edge to have just one component? 34 | 35 | DFS: 36 | 37 | TC: O(E+V) 38 | SC: O(E+V) //no accounting for adj list, but for the stack 39 | 40 | BFS: 41 | 42 | TC: O(V+E) 43 | SC: O(V) //no accounting for adj list 44 | 45 | union find path compression || rank: 46 | 47 | TC: O(V+logE) //no accounting for adj or directly calling on the edges, list otherwise O(E+V) 48 | SC: O(V) //no accounting for adj list 49 | 50 | union find path compression + rank: 51 | 52 | TC: O(E⋅α(n)) //no need of an adj list, otherwise O(E+V), with parent optimization on the fly 53 | SC: O(V) //no accounting for adj list 54 | 55 | ### Links & material 56 | The proposed problem il similar to LeetCode [323: Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/description/). (for premium users) 57 | 58 | * [323 - Number of Connected Components in an Undirected Graph on leetcode.ca](https://leetcode.ca/2016-10-18-323-Number-of-Connected-Components-in-an-Undirected-Graph/) 59 | * [323 - Number of Connected Components in an Undirected Graph NeetCode youtube solution](https://www.youtube.com/watch?v=pXNIwu3uDnA) 60 | 61 | 62 | ## Interview solution 63 | 64 | ```py 65 | def dfs(graph: list[list[int]], node: int, visited: set): 66 | visited.add(node) 67 | for connected in graph[node]: 68 | if connected not in visited: 69 | dfs(graph, connected, visited) 70 | 71 | def determine_connectiveness(n: int, edges: list[list[int, int]]) -> bool: 72 | graph = [list() for _ in range(n)] 73 | for from, to in edges: 74 | graph[from].append(to) 75 | graph[to].append(from) 76 | 77 | visited = set() 78 | dfs(graph, 0, visited) 79 | return len(visited) != n 80 | 81 | n = 5, edges = [[0,1],[1,2],[3,4]] 82 | 83 | ``` 84 | 85 | graph = [[1], [0, 2], [1], [4], [3]] 86 | visited = {0, 1, 2} 87 | return True 88 | 89 | O(N + M) M: size(edges), N 90 | 91 | ## Follow Up 92 | 93 | ```py 94 | def dfs(graph: list[list[int]], node: int, visited: set): 95 | visited.add(node) 96 | for connected in graph[node]: 97 | if connected not in visited: 98 | dfs(graph, connected, visited) 99 | 100 | def determine_connectiveness(n: int, edges: list[list[int, int]]) -> bool: 101 | graph = [list() for _ in range(n)] 102 | for from, to in edges: 103 | graph[from].append(to) 104 | graph[to].append(from) 105 | 106 | visited = set() 107 | num_components = 0 108 | new_edges = [] 109 | for node range(n): 110 | if node not in visited: 111 | num_components += 1 112 | dfs(graph, node, visited) 113 | if node != 0: 114 | new_edges.append([node, 0]) 115 | assert len(new_edges) == num_components - 1 116 | return new_edges 117 | 118 | ``` 119 | 120 | O(NM) 121 | O(M) 122 | 123 | Union find use: 124 | 125 | ```py 126 | class UFDS: 127 | def __init__(n: int): 128 | self.parents = [None for _ in range(n)] 129 | 130 | def find(x): 131 | if self.parent[x] is not None 132 | p = find(self.parent[x]) 133 | self.parent[x] = p 134 | return p 135 | return x 136 | 137 | def same_set(x, y): 138 | return find(x) == find(y) 139 | 140 | def union(x, y): 141 | x, y = find(x), find(y) 142 | self.parent[x] = y 143 | 144 | 145 | 146 | 147 | def determine_connectiveness(n: int, edges: list[list[int, int]]) -> bool: 148 | ufds = UFDS(n) # O(N) 149 | for from, to in edges: 150 | ufds.union(from, to) # amort. O(1) 151 | return not all(something(map(ufds.find, range(n)))) # O(N) 152 | 153 | ``` 154 | 155 | Time O(N + M) 156 | Memory O(N) 157 | --------------------------------------------------------------------------------