Exam Preparation Algorithms Course: Sample Exam SoftUni Team Technical Trainers Software University
2 The practical exam checks your skills to design and implement algorithms efficiently 4 problems for 6 hours Graphs, dynamic programming, recursion, combinatorics, greedy, … Automated judge system with real-time feedback: Algorithms: Practical Exam
3 Group-Permutations: we are given n capital Latin letters. Generate all their permutations, so that the same letters stay together. Example: Example: n = 7, letters = { B, C, A, B, A, C, B }. Result: AABBBCC AACCBBB BBBAACC BBBCCAA CCAABBB CCBBBAA Sample Problem #1 – Combinatorics
4 Remove duplicate letters: { B, C, A, B, A, C, B } { A, B, C } Permute the letters and replace each letter with its original number of occurrences: ABC AABBBCC ACB AACCBBB BAC BBBAACC BCA BBBCCAA CAB CCAABBB CBA CCBBBAA Group-Permutations – Solution
5 Bridges: we have two sequences of points (natural numbers): north[0 … n-1] and south[0 … m-1] Connect with bridges as many as possible equal numbers from both sequences so that there are no crossing bridges Print the max number of bridges Sample Problem #2 – Dynamic Programming
6 Bridges – Solution Assume f(x, y) = max bridges using north[0…x] and south[0…y] f(x, y) = 0 when x < 0 or y < 0 f(x, y) = max ( f(x-1, y), f(x, y-1) ; 1 + f(x-1, y), 1 + f(x, y-1) when north[x] == south[y] )
7 You are given a directed graph. Find all cycles of length 3. Example: Cycles (in lexicographical order): {0 1 6}, {0 13 2}, {0 13 9}, {4 6 10}, {6 11 8} Sample Problem #3 – Graphs
8 For each 3 vertices {u, v, w} check if they form a loop To skip the duplicates, require that u < v and u < w To improve performance, use sorted adjacency list for each vertex Cycles of Length 3 – Solution for u = 0 … n-1 for v = children-in-increasing-order(u) where u < v for w = children-in-increasing-order(v) where u < w if edge-exists(w u) print { u v w } u v w
9 Line Inverter: We are given a board of size n x n Each cell holds black ( 0 ) or white ( 1 ) cell At each step we can invert a row or invert a column Find the minimal number of inversions to make the board black Or print -1 if this is not possible Sample Problem #4 – Other
10 Line-Inverter problem has a simple greedy solution: 1. Invert all rows that have a white cell in the first column 2. Invert all columns that have a white cell in the first row 3. If the board is black, we have solution, otherwise no solution Another solution is found by: First inverting the first column, then running the previous algorithm Take the better of these two algorithms Line Inverter: Solution
? ? ? ? ? ? ? ? ? Exam Preparation
License This course (slides, examples, labs, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 12 Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA licenseFundamentals of Computer Programming with C#CC-BY-SA "Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA licenseData Structures and AlgorithmsCC-BY-NC-SA
Free Software University Software University Foundation – softuni.orgsoftuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg softuni.bg Software Facebook facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity Software YouTube youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bgforum.softuni.bg