Amazon as all other companies doesn’t share information about the tasks they use during their interviewing process. But internet is full of lists of the tasks which collecting the people who participating in the interview. IMHO the most comprehensive and the most actual list is collected here. So I decided it would be nice idea to collect solutions for these task in one set of articles. I’m going to write detailed explanations for each solution how it works and why I believe it is the best. I will use C++17 programming language. …


Some time ago I found on Codility website a wonderful set of training tasks which covers many subjects of questions using by big companies on interview. Codility calls this set of tasks the “Lessons”. Each lesson has a link to a PDF file with theoretical description subject of the lesson. The tasks are intended to show examples of the practical application of knowledge obtained from the description, but almost every task has a solution much better than what the authors of the lessons imply, this solution usually uses a different approach, not described in the topic of the lesson. I…


Microsoft, as all other companies, doesn’t share information about the tasks they use during their interviewing process. But internet is full of lists of the tasks which collecting the people who participating in the interview. IMHO the most comprehensive and the most actual list is collected here.

So I decided it would be nice idea to collect solutions for these task in one set of articles. I’m going to write detailed explanations for each solution how it works and why I believe it is the best. I will use C++17 programming language. …


Description:

Solution:

It looks like this task is not about algorithms, this is just about writing a simple parser which breaking the given string to parts and writing a function which finds and counts all substrings in the given string.

The only interesting subject which can be discussed in this task is implementation of algorithm for finding substrings. But the task doesn’t require to implement this algorithm, the strings are short, we have no special requirements for search of substrings, so we can use implementation from the standard library. The only thing we should keep in mind is that complexity of this…


Description:

Give you one sorted array, please put them into n buckets, we need to ensure we get n sub array with approximately equal weights.

Example:

input {1, 2, 3, 4, 5} n = 3

output [[5],[1,4],[2,3]];

Solution:

Generally this task look like a typical partition problem https://en.wikipedia.org/wiki/Partition_problem

Unfortunately this description looks unclear or too general because it doesn’t describe what exactly “approximately equal weights” means, so I don’t believe this question will be asked on an interview this way. You will need to ask the interviewer about details and choose the right approach.

There are many approaches to solve this problem…


Description:

Related problems:

Solution:

This task is very simple. We just need go through the given array and check that the difference between three simultaneous items is equal. For example in array 1, 2, 3 the difference between 1 and 2 and between 2 and 3 is 1. That is in term of the task the movement of the particle was stable. Each time when they are equal we increase the counter of the periods of time when the movement is stable and add it into a counter of all periods. We need two counters in order to count periods…


Description:

We are given a string S of length N consisting only of letters ‘A’ and/or ‘B’. Our goal is to obtain a string in the format “A…AB…B” (all letters A’ occur before all letters ‘B’) by deleting some letters from S. In particular, strings consisting only of letters A’ or only of letters ‘B* fit this format.

Write a function:

int solution(string & s);

that, given a string S, returns the minimum number of letters that need to be deleted from S in order to obtain a string in the above format.

Examples:

1. Given S = “BAAABAB”, the function…


Description:

Given an integer n, return any array containing n unique integers such that they add up to 0.

Example 1:

Input: n = 5
Output: [-7,-1,1,3,4]
Explanation: These arrays also are accepted [-5,-1,1,2,3], [-3,-1,2,-2,4].

Example 2:

Input: n = 3
Output: [-1,0,1]

Example 3:

Input: n = 1
Output: [0]

Constraints:

  • 1 <= n <= 1000

Solution:

This task is very simple. We don’t need to play with a random number generator. We just need to make an array of different numbers. For example sequence 012345 contains different and unique numbers. So we just generate two the same sequences of numbers…


Description:

There are N balls positioned in a row. Each of them is either red or white. In one move we can swap two adjacent balls. We want to arrange all the red balls into a consistent segment. What is the minimum number of swaps needed?

Write a function:

int solution(string s);

that, given string S of length N built from characters “R” and “W”, representing red and white balls respectively, returns the minimum number of swaps needed to arrange all the red balls into a consistent segment. If the result exceeds 109, return -1.

Examples:

1. Given S = “WRRWWR”…


Description:

Alexa is given n piles of equal or unequal heights. In one step, Alexa can remove any number of boxes from the pile which has the maximum height and try to make it equal to the one which is just lower than the maximum height of the stack. Determine the minimum number of steps required to make all of the piles equal in height.

Example 1:

Input: piles = [5, 2, 1]
Output: 3
Explanation:
Step 1: reducing 5 -> 2 [2, 2, 1]
Step 2: reducing 2 -> 1 [2, 1, 1]
Step 3: reducing 2 -> 1 [1, 1, 1]
So final number of…

Alexander Molchevskyi

Looking for an interesting project to join. https://www.linkedin.com/in/molchevsky/

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