Today in the morning there was a very interesting post about what code you need to write at the interview and what skills to demonstrate. The test task was this.
Write a function on TypeScript that, for a given array of numbers, displays a text string of ranges:
getRanges([0, 1, 2, 3, 4, 7, 8, 10]);
Unfortunately, this post is no longer available, and I will not restore the solution given now. But this solution was so beautiful, just as extremely poorly readable for the
average programmer who was slightly tortured by the management by the end of the deadline. It was an almost single-line write-only Perl-style script.
And the question is, how to evaluate this code? On the one hand, he demonstrates the high technical level of the candidate. On the other hand, it also demonstrates some misunderstanding of team development processes with programmers of quite different levels of training.
I sketched my decision in about 10 minutes. It looks like this:
class Interval { start: number; stop: number constructor(start: number, stop: number) { this.start = start; this.stop = stop; } toString(arr: number[]) { let text: string; text = arr[this.start].toString(); if (this.start < this.stop) { text += '-' + arr[this.stop].toString(); } return text; } } function getRanges(arr: number[]) {
The algorithm here is this:
- in the first step we select intervals from an array of numbers
- in the second step we convert the intervals to strings
But then I looked at my decision. And not so that it also looked perfect. Somewhat verbose, an additional entity βIntervalβ is introduced. But still, I am convinced that this decision is much easier to read and understand.
But here is the question. How will this decision be evaluated in an interview from the point of view of an olympiad programming fan?
In general, I would like to continue this discussion.
UPD 1. Additions to the task. The input array is sorted.
UPD 2. The original post is again available,
habr.com/en/post/470407