My friend was drawing on the board while I looked on. We were in different teams, each assigned a project. His program currently took 5 hours to execute. The requirement stated one hour. As the ‘algorithm enthusiast’, I was called for help.
The program was to find the friends of all users in a social network. After explaining the use-case, my friend showed me his approach. I was very determined to find an optimization, hence I did. Immediately.
“An adjacency matrix?! No, use an adjacency list. The execution time will reduce in your case to O(N). Or you could use an adjacency tree! …
Imagine you’re the engineer responsible for building Reddit from the ground up. Walk me through how you would design the system to support the following functionality:
Think about the core components that you’ll need, and then dive deeper into them. What kind of database would you use? …
Sum Root to Leaf Numbers is an interesting problem from LeetCode. The problem is of medium difficulty and is about binary trees. This post is an explained solution to the problem.
I assume that you’re familiar with Python and the concept of binary trees. If you’re not, you can read this article to get started.
A man starts from the north-west (top-left) corner of the land and begins walking towards the center of it, in a spiral manner. As the man moves, he keeps track of the number of trees growing on each block of land and records the counts on his journal until he reaches his destination. This list of numbers is the key to unlock treasures hidden there.
You need to be faster than him. Satellite imagery has already provided you the count of trees on each block of land. …
You can assume there will be exactly 1 solution. Each element in the array can only be used once.
Array of integers is [2, 7, 9, -2].
The pair that sums up to 0 is (2, -2).
A list of numbers, separated by space.
2 7 9 -2
2 numbers from the array that sum up to 0.
We will sort the 2 numbers for you so that you can easily compare with our expected output.
You’re given a list of integers and a sum, S.
Array: 1 5 3 4 3
Insert, in front of each number, 1 of the 2 symbols,
-, so that the resulting sum adds up to S.
Write a function that counts all such possibilities.
In the example above, there are 3:
+1 +5 +3 -4 +3
-1 +5 +3 +4 -3
-1 +5 -3 +4 +3
Input: S followed by the list of integers.
Output: The number of possibilities where the sum adds up to S.
1 5 3 4 3Output…
The original linked list is as follows:
3 -> 5 -> 2 -> 4The reversed linked list has its arrows changed in direction:
3 <- 5 <- 2 <- 4which means
4 -> 2 -> 5 -> 3
Don’t create new nodes. Instead, reuse the original linked list, and modify the arrow directions in-place.
The linked list’s node values, separated by commas.
3 5 2 4
The reversed linked list’s node values, separated by commas.
4 2 5 3
A linked list can be split into the head node and the tail, which is also a linked list but headed by the second node. …
Given a string, sort it in decreasing order based on the frequency of characters.
'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.
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.
"bbaA" is also a valid answer, but "Aabb" is incorrect. …