Problems


November 30, 2022

Maximum average subtree

We will traverse the tree in post-order fashion. For each node, we will calculate the sum of all nodes in its subtree and the number of nodes in its subtree. Then we will check if the average of the...

Read

November 30, 2022

Remove duplicates from an unsorted linked list

We will use a hashmap to count the frequency of each node in the linked list. Then we will traverse the linked list again and remove the nodes that have frequency greater than 1. Time complexity:...

Read

November 30, 2022

Count nodes equal to average of subtree

We will traverse the tree in post-order fashion. For each node, we will calculate the sum of all nodes in its subtree and the number of nodes in its subtree. Then we will check if the average of the...

Read

November 30, 2022

Unique number of occurrences

We will count the number of occurrences of each number in the array. We will then check if the number of occurrences is unique using a hash set. Time complexity: O(n), n is the length...

Read

November 30, 2022

Unique word abbreviation

We will use a dictionary to store the count of each abbreviation, and a set to store the words that have been added to the dictionary. If the word is already present in the set, we will return false....

Read

November 30, 2022

Find all lonely numbers in the array

We will count the occurrences of each number in the array and return the numbers that have only one occurrence and no adjacent number is present in the list. Time complexity: O(n), n...

Read

November 30, 2022

Sparse matrix multiplication

We will iterate over the non-zero elements of the first matrix and multiply them with the corresponding elements of the second matrix. We will store the result in a dictionary and return the result....

Read

November 30, 2022

Verify preorder sequence in binary search tree

Using stack for storing the current node as a check point for the next element. Once the current is larger than the last element in the stack, we know we should take it as the right node. The last...

Read

November 29, 2022

Inorder successor in BST

As we are traversing a binary search tree, we can use it's property to find the inorder successor. The inorder successor of a node is the leftmost node in the right subtree. If the node does not have...

Read

November 29, 2022

Count univalue subtrees

We will use postorder traversal to count the number of univalue subtrees. We will also use a helper function to check if the current node is a univalue subtree. Time complexity: O(n),...

Read
... 26 27 28 29 30 ...