Problems
July 21, 2022
Prime number of set bits in binary representation
This is a very strait forward problem. We will have 2 helper function, one is for counting the number of bits in an integer and another for checking if the number is prime or not. Then we iterate...
ReadJuly 20, 2022
Binary tree inorder traversal
We will traverse the tree with recursive DFS, as it is inorder traversal, we first traverse the left subtree then append the root, and lastly we traverse the right subtree. Time Complexity:...
ReadJuly 20, 2022
Binary tree postorder traversal
We will traverse the tree with recursive DFS, as it is postorder traversal, we first traverse the left subtree, the the right subtree and then append the root to our result. Time Complexity:...
ReadJuly 20, 2022
Binary tree preorder traversal
We will traverse the tree with recursive DFS, as it is preorder traversal, we first append the root, then the left subtree and then right subtree.
Time Complexity: O(n)
Space...
July 20, 2022
Kth largest element in a stream
We will create a min heap from the elements of the stream. We will make sure heap size is always k. That means the smallest element will ve at the heap's top and the largest element will be at the...
ReadJuly 20, 2022
N-ary tree postorder traversal
We will traverse the tree with recursive DFS, as it is postorder traversal, we first traverse all the children and then append the root to our result.
Time Complexity: O(n)
Space...
July 20, 2022
N-ary tree preorder traversal
We will traverse the tree with recursive DFS, as it is preorder traversal, we first append the root to our result and then traverse all the children.
Time Complexity: O(n)
Space...
July 19, 2022
Binary search
The problem itself is called binary search, all we need to do a classic binary search. We will choose a mid, if the target value is greater than the mid value, we move our left pointer to mid or if...
ReadJuly 19, 2022
Evaluate reverse polish notation
We can go through each character and put this on a stack. When we find any arithmetic symbol, we pop last 2 element for the stack, do the operation and push back the value to the stack. After the...
ReadJuly 19, 2022
Linked list cycle
We will take two pointer, fast and slow. Fast goes twice as fast as slow. Then we run both pointers at the same time, if there is a cycle, then this two pointers will meet, else we will goes to the...
Read