What code should be shown at the interview

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]); // 0-4,7-8,10 getRanges([4, 7, 10]); // 4,7,10 getRanges([2, 3, 8, 9]); // 2-3,8-9
      
      





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[]) { // find start-stop intervals in the sequence let intervals : Interval[] = []; let start = 0; for (let i = 1; i < arr.length; i++) { if (arr[i] - arr[i - 1] > 1) { intervals.push(new Interval(start, i - 1)); start = i; } } intervals.push(new Interval(start, arr.length - 1)); // convert intervals to the string let out : String = ""; for (let i = 0; i < intervals.length; i++) { out += intervals[i].toString(arr); if (i < intervals.length - 1) { out += ','; } } console.log(out); } getRanges([0, 1, 2, 3, 4, 7, 8, 10]); // 0-4,7-8,10 getRanges([4, 7, 10]); // 4,7,10 getRanges([2, 3, 8, 9]); // 2-3,8-9
      
      





The algorithm here is this:





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



All Articles