Are complex algorithms relevant in the real world?

Image for post
Image for post

INTERNSHIP PROJECT

Hackerrank, 2019.

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! …


Image for post
Image for post

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:

Requirements:

  • Users can make posts in different forums (i.e. “subreddits”)
  • Users can attach images to their posts
  • Users can upvote or downvote posts
  • Users can add comments to posts
  • Users can see a feed of posts sorted by ranking or recency

Constraints:

  • We want to support a large volume of users (millions) viewing and posting content

Hints

Think about the core components that you’ll need, and then dive deeper into them. What kind of database would you use? …


Image for post
Image for post
Photo by Chris Ried on Unsplash

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.

Image for post
Image for post

You need to be faster than him. Satellite imagery has already provided you the count of trees on each block of land. …


You’re given an array of integers. Write a function that returns a pair of numbers such that they sum up to zero.

Image for post
Image for post
[Alternate problem with a target]

You can assume there will be exactly 1 solution. Each element in the array can only be used once.

Example:

Array of integers is [2, 7, 9, -2].
The pair that sums up to 0 is (2, -2).

Input:
A list of numbers, separated by space.

2 7 9 -2

Output:
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.

-2 2

Solution:

Observe

  1. For a number A, we want to know if such a number B exists, such that A + B = 0. …

You’re given a list of integers and a sum, S.

Image for post
Image for post

For example:

Array: 1 5 3 4 3
S: 8

Insert, in front of each number, 1 of the 2 symbols, + or -, 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.

Input:
8
1 5 3 4 3
Output…

Image for post
Image for post
Photo by Christopher Gower on Unsplash

Problem Statement:

In older phones, the keypads are used to type both numbers and letters, as follows:


You’re given a linked list. Write a function to reverse it.

Image for post
Image for post

Example:

The original linked list is as follows:
3 -> 5 -> 2 -> 4
The reversed linked list has its arrows changed in direction:
3 <- 5 <- 2 <- 4
which means
4 -> 2 -> 5 -> 3

Don’t create new nodes. Instead, reuse the original linked list, and modify the arrow directions in-place.

Input:
The linked list’s node values, separated by commas.

3 5 2 4

Output:
The reversed linked list’s node values, separated by commas.

4 2 5 3

Solution: Reverse a Linked List

Observe

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. …


Image for post
Image for post

Question:

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. …

A quick primer for advanced JavaScript concepts like primitives, hoisting, closures, and this binding.

Image for post
Image for post

Some people called Javascript a virus and if that’s true it means developers are the host. No matter what you label yourself as today you have to face the reality that one day you may join the hordes of Javascript developers out there and when that day comes you need to be prepared by understanding the practical fundamental concepts of the JavaScript programming language. Today's blog is not about victory it’s about survival.

In this blog, we will look at many of the fundamental features of the language as they relate to practical programming but more importantly, I want to help you survive most of the weirder parts of javascript that might trip you up or that you might find on an interview question and many of these concepts like closures and hoisting can be easily understood by removing the head or destroying the brain. …

About

S7rthak

Software Engineer @Vedantu, Former Intern @Hackerrank, GSoC @Wikimedia, Former Intern @Vedantu, Codeforces(Expert)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store