Bloomberg Leetcode Questions
What are the 5 steps to solving technical questions?
1. Ask questions to resolve ambiguity 2. Design an algorithm 3. Pseudocode your algorithm 4. Implement your algorithm 5. Test your code and "carefully" fix any mistakes
https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string s, find the length of the longest substring without repeating characters. Example 1 Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2 Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3 Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Questions to ask: -Maximum length of string? -What characters are allowed/not allowed? Algorithm: -Brute force -Sliding window - move through the length of a string with [i,j). When a new char is encountered, add it to set. When duplicate is encountered, remove it from set. -Sliding window optimized with hashing - store the char as a key, then the index as j + 1 (current index + 1) so we can "jump" to the next index that does not contain duplicate. Implement: https://repl.it/@immxalan/longestsubstring#main.py
https://leetcode.com/problems/two-sum/submissions/ Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Questions to ask: -What is the value range for the integers in nums? -What is minimum and maximum length for the array, nums? -What is the value range for the target? Algorithm: -Brute force: nested for loop to solve -Follow up: Use a map to store values that you have have already iterated over and check if: target - current = (value inside of your map) If the value is inside your map, then you know have the two numbers that add up to target. Implement: https://repl.it/@immxalan/2-sum#main.py
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Follow up: You may only use constant extra space. Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem. https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
Questions: -Min/max number of levels to the tree? -Min/max values for each node? -Can there be duplicate values? Algorithms: -Perform BFS to evaluate tree level by level -Use a queue and initialize with root -Use a while loop to iterate through queue as long as there's a node in queue -Pop node from queue, add it's children to queue, and set next pointer on children Follow up: -Will still be doing BFS level by level -If performing with constant space, use a while loop that is dependent on "level" variable, which we set to leftmost node (e.g. root for level 1, root.left for level2) - loop breaks when level.left is None -Set a separate head variable to leftmost node, create another while loop dependent on head not being None -As you move through each node in a level via BFS - set next pointers for children of node you are at -Set head = head.next to move BFS -Once head is None, set level to level.left to move to next level Implement: https://repl.it/@immxalan/next-right-pointers#main.py
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m + n such that it has enough space to hold additional elements from nums2. Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Example 2: Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1]
Questions: -Min/max of m and n -Min/max values for integers in nums1 and nums2 Algorithms: Implement:
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false
Questions: -What are valid characters? -Min/Max length of strings? Algorithms: -Two-pass with hash-map: Pass string "s" and add chars with the number of times it occurs as a key-value pair to hash-map. Run another loop through string "t" and subtract from hash map for every char, if it drops below 0 you know the anagram is false. Implement:
https://leetcode.com/problems/3sum/ Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Notice that the solution set must not contain duplicate triplets. Example 1: Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]]
Questions: What is the min/max length of array nums? What is the range of values for integers in nums? Algorithms: 3 ways to approach this problem, easier solutions requires you sort it first. -Sort the array, then use 2 pointers, helper fn, and set -Sort the array, then use complement = -nums[i] - nums[j], helper fn, and set -No-sort/Don't copy array Implement: