Best solutions for Microsoft interview tasks. Particle Velocity.


Related problems:


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 which include other periods. Pay attention of the note in the task description “Note that some periods of time might be contained in others”. Also pay attention that we pass through the array only once. Despite the fact that we have two nested arrays we use only one iterator “i”. So complexity of the task is O(N).

Therefore the solution code is trivial:

int solution(const vector<int> & v) {
int total_periods = 0, v_size = v.size();
for (int i = 0; i < v_size; ++i) {
for (int count = 0;
i + 2 < v_size && v[i + 1] - v[i] == v[i + 2] - v[i + 1];
++i) {
total_periods += ++count;
return total_periods < 1000000000 ? total_periods : -1;

Repository with the full project you can find here:

Return to the table of contents.



Looking for an interesting project to join.

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