PinnedLearn Data Structures And Algorithms With Swift 5.6Pick up and Master your DSA (Data Structure & Algorithms) more simply with Learn Data Structures And Algorithms With Swift 5.6. — Find this book on amazon: Australia, Brazil, Canada, France, Germany, India, Italy, Japan, Mexico, Netherlands, Spain, United Kingdom, United States. During my career, I passed interviews at Agoda, Google, Meta, TikTok, etc. I wrote down all my preparation about data structures and algorithms with plenty of interview questions with answers.Data Structures3 min readData Structures3 min read
Published in Level Up Coding·17 hours agoMember-only52. Reverse a Simple Linked ListQuestion: Write a function that reverses a simple LinkedList.. — For example, given [12, 24, 33, 43, 52], you should return [52, 43, 33, 24, 12] Hints: - This can be built linearly, -You can do it with a constant space complexity, - You need to use three pointers current, previous and next nodes. Solution: func reverseLinkedList<T>(root: LinkedListNode<T>?) -> LinkedListNode<T>? {…Data Structures2 min readData Structures2 min read
Published in Geek Culture·3 days agoMember-only51. Resolve All Tasks In OrderQuestion: Write a function that returns the tasks to follow in a particular order that allows you to finish all tasks depending on their prerequisites; if such order does not exist, return nil. — For example, given [“TSK180”: [“TSK120”, “TSK150”, “TSK170”], “TSK150”: [“TSK120”], “TSK120”: [], “TSK170”: [“TSK150”], “TSK200”: [“TSK180”]], you should return [“TSK120”, “TSK150”, “TSK170”, “TSK180”, “TSK200”].Data Structures3 min readData Structures3 min read
Published in Level Up Coding·Mar 13Member-only50. Remove sorted array duplicates (in-place)Question: Write a function that removes duplicates from a sorted array in-place. — For example, given [1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 6], return [1, 2, 3, 4, 5, 6], For example, given [6, 6, 6, 6], return [6], For example, given [1, 2, 3, 4, 5, 6], return [1, 2, 3, 4, 5, 6]. Hints: - This can be…Data Structures2 min readData Structures2 min read
Published in Geek Culture·Mar 9Member-only49. Build a Binary Tree (from pre-order and in-order traversals)Question: Write a function that rebuilds a binary tree from its pre-order and in-order traversals. — For example, given the pre-order traversal [32, 23, 41, 15, 32, 66, 71] And the in-order traversal [41, 23, 15, 32, 66, 32, 71], you should return: 32 / \ 23 32 / \ / \ 41 15 66 71 Hints: - This can be built recursively, - You can browse…Data Structures3 min readData Structures3 min read
Published in Geek Culture·Mar 6Member-only48. Same probability: Random 7 / Random 5Question: Write a function random7() that returns with the same probability numbers between 1…7 based on another random function random5Base() that returns with the same probability numbers in 1…5. — Hints: - You can use a squared matrix (5 * 5) with numbers only from 1 to 7 spread equally and fill with 0 for the remaining ones, - Pick x and y from the random5() function; if not 0, return that number, or retry.Data Structures3 min readData Structures3 min read
Published in Level Up Coding·Mar 3Member-only47. Trapped Water QuantityQuestion: Write a function that returns the quantity of water possibly trapped from a given height list. — For example, given [3, 0, 1, 2, 0, 2, 0], you should return 5, For example, given [0, 2, 3, 0, 1, 4, 0, 1], you should return 6. Hints: - You need to keep track of the maximum you saw, - Compare max left, max right, and local heights. Solution: func…Data Structures3 min readData Structures3 min read
Published in Geek Culture·Feb 28Member-only46. 2 Stacks QueueQuestion: Write a queue (FIFO) composed of 2 stacks (LIFO). — Hints: - You should use one stack to enqueue only, - You should use the other stack to dequeue; if empty, refill it with the enqueue stack. Solution: class QueueWith2Stack<T> { // 1. fileprivate var stackIn = Stack<T>() fileprivate var stackout = Stack<T>()…Data Structures2 min readData Structures2 min read
Published in Level Up Coding·Feb 25Member-only45. 2 Prime Numbers TargetQuestion: Write a function that returns two prime numbers that the sum equals to a given even integer target. — For example, given 4, you should return (2, 2), For example, given 10, you should return (3, 7), For example, given 5, you should return nil. Hints: - Build first the prime numbers up to the target. - Just simply loop thought n and compare primes. Solution: func primeNumberSum(target: Int) -> (Int…Data Structures3 min readData Structures3 min read
Published in Geek Culture·Feb 23Member-only44. Power Set (all possible subsets)Question: Write a function that returns the power set of a given set (all subsets possible from a given set). — For example, given [1], you should return [[], [1]], For example, given [1, 2], you should return [[], [2], [1], [1, 2]]. Hints: - How many subsets are possible for a set of N elements?, - What about using the binary representation of all numbers from 0 to 2 ^ n…Data Structures3 min readData Structures3 min read