Posts

MockInterview:722. Remove Comments

Medium Given a C++ program, remove comments from it. The program  source  is an array where  source[i]  is the  i -th line of the source code. This represents the result of splitting the original source code string by the newline character  \n . In C++, there are two types of comments, line comments, and block comments. The string  //  denotes a line comment, which represents that it and rest of the characters to the right of it in the same line should be ignored. The string  /*  denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of  */  should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string  /*/  does not yet end the block comment, as the ending would be overlapping the beginning. The first effective comment takes precedence over others: if the string  //  occurs in a blo...

MockInterview:168 Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... Example 1: Input: 1 Output: "A" Example 2: Input: 28 Output: "AB" Example 3: Input: 701 Output: "ZY" Wrong code: test case: 52 class Solution { public String convertToTitle(int n) { char[] map = {' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; StringBuilder sb = new StringBuilder(); while(n!=0){ int last = n % 26; if(last == 0){ last = 26; char lastC = map[last]; ...

MockInterview:48. Rotate Image

You are given an  n  x  n  2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the image  in-place , which means you have to modify the input 2D matrix directly.  DO NOT  allocate another 2D matrix and do the rotation. Example 1: Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2], [9,6,3] ] Example 2: Given input matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], rotate the input matrix in-place such that it becomes: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ] wrong code: class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i = 0; i < n ; i++){ rotateOneLayer(matrix,0,n-1); } } public void rotateOneLayer(int[][] matrix, int start, int end){ fo...

MockInterview:451. Sort Characters By Frequency

Share Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. Example 2: Input: "cccaaa" Output: "cccaaa" Explanation: Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer. Note that "cacaca" is incorrect, as the same characters must be together. Example 3: Input: "Aabb" Output: "bbAa" Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect. Note that 'A' and 'a' are treated as two different characters. class Solution { public class CharFre{ public char c; public int fre; public CharFre(ch...

MockInterview: 39 Combination Sum

Given a  set  of candidate numbers ( candidates )  (without duplicates)  and a target number ( target ), find all unique combinations in  candidates  where the candidate numbers sums to  target . The  same  repeated number may be chosen from  candidates  unlimited number of times. Note: All numbers (including  target ) will be positive integers. The solution set must not contain duplicate combinations. Example 1: Input: candidates = [2,3,6,7], target = 7 , A solution set is: [ [7], [2,2,3] ] Example 2: Input: candidates = [2,3,5] , target = 8, A solution set is: [   [2,2,2,2],   [2,3,3],   [3,5] ] pratice code: class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { return helper(candidates,target,0); } public List<List<Integer>> helper(int[] candidates, int target, int index){ int num = candid...

701. Insert into a Binary Search Tree

701. Insert into a Binary Search Tree Medium Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them. For example,  Given the tree: 4 / \ 2 7 / \ 1 3 And the value to insert: 5 You can return this binary search tree: 4 / \ 2 7 / \ / 1 3 5 This tree is also valid: 5 / \ 2 7 / \ 1 3 \ 4 Solution: A new node is always inserted at leaf node, we start search the key from root until we hit a leaf node.once the leaf node is found, the new node is inserted as its child. class So...

450. Delete Node in a BST

Medium Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the node is found, delete the node. Note:  Time complexity should be O(height of tree). Example: root = [5,3,6,2,4,null,7] key = 3 5 / \ 3 6 / \ \ 2 4 7 Given key to delete is 3. So we find the node with value 3 and delete it. One valid answer is [5,4,6,2,null,null,7], shown in the following BST. 5 / \ 4 6 / \ 2 7 Another valid answer is [5,2,6,null,4,null,7]. 5 / \ 2 6 \ \ 4 7 Solution: 1) If the node is leaf, just remove this node, remove the connection between its parent and this node. 2) If the node has one child, replace this node with its child. connect its parent with its child. root's parent's left or right point to root's ...