Studija slučaja tehničkog telefonskog razgovora: Kako udvostručiti niz u JavaScript-u

Tehnički zasloni telefona presudan su korak u procesu tehničkog razgovora. Često ćete prolaziti kroz tehnički zaslon telefona odrediti hoćete li biti pozvani na razgovor na licu mjesta.

Tehnički zasloni telefona mogu biti teški jer morate razmišljati naglas dok rješavate problem, a da pritom ne budete osobno prisutni sa svojim anketerom. Kada intervjuirate nekoga putem telefona ili videa, može vam biti teško da prođe cijela vaša prisutnost. Obično ćete raditi u zajedničkom uređivaču, tako da će, dok rješavate problem, ispitivač moći samo čuti i vidjeti što pišete. Mnogi ljudi teže komuniciraju na ovaj način nego lično.

Dobra vijest je da su tehnički zasloni telefona nešto u čemu možete vježbati i poboljšati se. Kao i svaka vještina, što ih više budete radili, to ćete biti bolji. Na kraju ćete početi vidjeti rezultate jer ćete biti pozvani na razgovor na licu mjesta sa sve više i više tvrtki.

Iako su svi tehnički telefonski razgovori različiti, za većinu će biti potrebno razmišljati na nogama. Stoga je najbolji način pripreme jednostavno uvježbavanje rada na pitanjima. Možete sami proći kroz njih izgovarajući ih, a možete i vježbati s prijateljem. Ako vježbate samostalno, mogli biste se i snimiti kako biste mogli preslušati snimku i provjeriti ima li smisla kako ste objasnili svoj misaoni proces.

Napokon, možete vježbati intervjuirajući tvrtke! Kad sam zadnji put razgovarao za novu ulogu, započeo sam s pronalaženjem tvrtki koje su me zanimale, ali ne bih se uzrujavao ako ne bih prošao pokraj tehničkog zaslona telefona. Na taj sam način još uvijek osjećao pritisak da se pripremim, ali očekivao sam da ću prvo nekoliko puta propasti. Tada je bilo manje razočaravajuće kad nisam prešao na sljedeću fazu.

U ovom postu prolazit ću kroz pitanje koje sam dobio na tehničkom zaslonu telefona kako bih vam dao okvir za pristup tim vrstama intervjua. Nadam se da je ovo korisno i pozdravljam vaše komentare i povratne informacije!

Uronimo.

Pitanje

Ovo je bilo stvarno pitanje koje sam dobio od anketara. Sviđa mi se ovo pitanje, jer postoji nekoliko načina za njegovo rješavanje. Način na koji ga rješavate odražava vaš stil programiranja i pomaže ispitivaču da procijeni jeste li sposobni za to radno mjesto ili ne.

Evo primjera pitanja za intervju:

Given an array, write a function that doubles the array.Example: given [1,2,3,4,5], your function should return [1,2,3,4,5,1,2,3,4,5].You could call it like so: myArray.double().

Odgovarajući na pitanje

Evo mojih pet koraka za rješavanje problema tijekom tehničkog zaslona telefona:

1. Razjasnite pitanje

2. Razmislite o malim test slučajevima, uključujući rubne slučajeve

3. Pseudo-kodirajte svoje rješenje (nije obavezno)

4. Prevedite svoj pseudo-kôd u stvarni kôd

5. Testirajte svoje rješenje pomoću test slučajeva koje ste ranije smislili

1. Razjasnite pitanje

Prvo što biste trebali učiniti kada dobijete ovakvo pitanje na intervjuu jeste postavljanje razjašnjenih pitanja.

U ovom je slučaju pitanje relativno jednostavno: razumijem da moram napisati funkciju koja uzima niz i vraća niz kojim je manipulirano. Razumijevanje unosa i izlaska funkcije rezultira onim što se često smatra potpisom funkcije.

2. Razmislite o malim test slučajevima, uključujući rubne slučajeve

Zatim ćete htjeti razmisliti o nekim manjim primjerima koji će vam kasnije poslužiti kao testni slučajevi:

// What happens when the given array is empty?[] => []
// What happens when the given array has only 1 element?[1] => [1,1]
// What happens when the given array has only 2 elements?[1,2] => [1,2,1,2]
// What happens when the given array has N elements?[1...N] => [1,2,3,4,5...N,1,2,3,4,5...N]

Razmišljanje o tim slučajevima prije nego što započnete s kodiranjem pomoći će vam potražiti i uspostaviti obrasce za ono što pokušavate riješiti. Također će vam pomoći da razmislite o složenosti prostora ili vremena, što će kasnije možda postati dodatno pitanje. Ovo također pomaže da budete sigurni da ste dovoljno razumjeli pitanje, jer vašem ispitivaču daje priliku da ispravi sve zablude.

3. Pseudo-kodirajte svoje rješenje (nije obavezno)

Sad kad ste razjasnili problem i smislili nekoliko primjera testnih slučajeva, vrijeme je da razmislite o stvarnom rješenju. Tu pseudo-kodiranje može dobro doći. Ako niste upoznati s pseudo-kodiranjem, ideja je da napišete ono što želite učiniti jednostavnim jezikom ili pojednostavljenom sintaksom koda prije ispisivanja radnog koda. To je način koji će vam pomoći organizirati svoje misli prije nego što skočite ravno u kod.

Pseudo-kodiranje može biti nevjerojatno učinkovito u smislu pomoći vam da ostanete na putu tijekom razgovora. Osobno to volim raditi, jer mi pomaže da ostanem organiziran. Ako ikad zapnem, mogu se vratiti na korake koje sam napisao pseudo-kodom da bih se vratio na pravi put.

Jednom sam imao telefonski intervju gdje sam napisao korake u pseudo-kodu prije pisanja stvarnog koda. Intervjuer mi je mogao pomoći da me usmjeri ukazujući na korak u mom pseudo-kodu koji sam trebao poduzeti sljedeći. U ovom slučaju, ispitivač je također spomenuo da nikada prije nije vidio nikoga da to radi i bio je nevjerojatno impresioniran. Dakle, pseudo-kodiranje također ima prednost u tome što pokazuje anketaru da ste organizirani i impresionira ih tim vještinama!

Dakle, vraćajući se na pitanje, evo nekoliko pseudo-kodova koje biste mogli napisati:

// Define a function that takes in an array// Loop over the array// Push each element from the array back into the array// Return the array

4. Prevedite svoj pseudo-kôd u stvarni kôd

Sad kad ste napisali pseudo-kôd, vrijeme je za kodiranje. Za ovo pitanje, prvo (netočno) rješenje koje sam smislio izgledalo je ovako:

var array = [1,2,3,4,5];
var double = function(array) {
 for (var i = 0; i < array.length; i++) { array.push(array[i]); }
 return array;
}
double(array);

Now, this seems pretty straightforward, right? However, there’s a small trick to this question that I only discovered by coding up my solution and trying to run it. That brings me to the final step!

5. Test your solution using the test cases you came up with earlier

If you’re an experienced programmer, you might easily spot the bug in my solution above. But it wasn’t until I ran my code that I realized I had created a dreaded infinite loop!

Why does this create an infinite loop? The array.length that I was using to know when my for loop would stop was dynamically increasing as I was pushing new elements into the array! So, when the for loop started, array.length was equal to 5. But after the first iteration of the for loop, array.length was equal to 6, and on and on ad infinitum.

However, there is a simple change that will make this solution work:

var array = [1,2,3,4,5];
var double = function(array) {
 var length = array.length;
 for (var i = 0; i < length; i++) { array.push(array[i]); }
 return array;
}
double(array);=> [1,2,3,4,5,1,2,3,4,5]

RUNTIME: O(n) = linear

With this change, I’m declaring a variable called length inside the scope of the function and then using that as the delimiter for my for loop. Even though my array size is now changing, the for loop still stops after the 5th iteration, because the length variable does not change when array.length changes.

Now I can test my code with the edge cases I came up with ealier and see that the results are as expected:

// Passing in an empty array yields an empty array correctly:[] => []
// Passing in an array with only 1 element yields the correct array with 2 elements:[1] => [1,1]
// Passing in an array with only 2 elements yields the correct array with 4 elements:[1,2] => [1,2,1,2]
// Passing in an array with 10 elements yields the correct array with 20 elements:[1,2,3,4,5,6,7,8,9,10] => [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]

Alternate solutions

The above is one way to solve this question, but there are a couple of other alternatives as well. Remember when I introduced the question above with the suggestion of calling the function by writing something like myArray.double()? If you’re familiar with object oriented programming, you may recognize this syntax. In this case, the general idea is that you would actually add an array method called double using the prototype chain, that you would then be able to call.

Here’s an example of how I could do that using the for loop structure from my original solution:

Array.prototype.double = function() { var length = this.length;
 for (var i = 0; i < length; i++) { this.push(this[i]); }
 return this;}
var myArray = [1,2,3,4,5];
myArray.double();=> [1,2,3,4,5,1,2,3,4,5]

By defining the function using the JavaSacript prototype chain, I don’t actually have to pass anything into it because I have access to the array that the method is being called on with this. To learn more about the this keyword, read the MDN docs.

Now, these solutions are great, but what about answering this question without using a for loop? One way is to use the built in JavaScript method forEach. This is the same idea as a for loop, but instead of us telling the program how to execute our code (imperative programming) we’re going to tell it what the result is (declarative programming). You can read more about imperative vs. declarative programming here.

Here’s an example of the same solution using forEach:

var array = [1,2,3,4,5];
var double = function(array) {
 array.forEach(function(value) { array.push(value); });
 return array;}
double(array);=> [1,2,3,4,5,1,2,3,4,5]

RUNTIME: O(n) = linear

Finally, here’s another solution to this problem, which I found with a few quick Google searches.

There is also a built in array method called concat that you can use:

var array = [1,2,3,4,5];
var double = function(array) { var doubled = array.concat(array);
 return doubled;}
double(array);=> [1,2,3,4,5,1,2,3,4,5]

RUNTIME: O(n) = linear

NOTE: If you’re wondering about Google searching during your phone screen, here’s my take after participating in more than a dozen technical phone screens: usually it’s completely acceptable.

Technical phone screens are often scheduled for 45 mins to 1 hour. Some of that time is reserved for the interviewer to ask questions about your experience, while some is also reserved for you to ask questions. The time you spend coding can be anywhere from 30–45 mins based on the company and interviewer.

In many cases, your interviewer will be able to help you with quick tips and small hints if you have a general idea about how to do something but need to look up the specifics. For example, I once had an interviewer who knew the regex I needed off the top of their head to perform a specific function, so I didn’t need to spend time figuring it out. This allowed the interview to continue more seamlessly.

However, I’ve also had experiences where an interviewer has asked me to refactor my original solution in a different way and explicitly said it was fine to look up documentation. This is usually the case, because many developers spend time daily reading or referencing docs. Being able to follow that same pattern in a technical phone interview is a good sign.

However, Googling for a solution during your interview can also be a time sink, especially if you’re not searching with just the right phrase (this is where the more you search, the better you will become).

For this specific example, if I had already known about JavaScript’s concat method, it might have come to mind when I was confronted with this problem. Then, Googling to remind myself of how concat worked would have been acceptable.

But if I had instead spent time Googling how to double an array before even trying to think through the problem myself, this might have been a red flag for the interviewer. Technical phone screens are a good way for an interviewer to get a sense of how you think, and it really depends what they are looking for in terms of the position they’re hiring for.

On the other hand, some companies will explicitly tell you that you’re not allowed to use Google for help, so in those cases, it’s best not to. Of course, if you’re unsure at all, ask your interviewer.

Conclusion

Why am I showing you all of these examples? As you can see, there is not just one single way to approach this problem. There are several approaches you can take, and how you approach the problem all depends on a combination of what your background is and how you think about problem solving. For me, I often gravitate toward loops since for loops were one of the original programming concepts I learned. But someone who’s used concat before might think of that right off the bat.

I thought this problem was a good example, because it seems relatively simple at first. However, there are ways to get tripped up (as you saw with my infinite loop above), and there are several solutions that demonstrate various levels of specific knowledge. Still, you could also solve this with a solid idea written in pseudo-code and some Googling.

Keep in mind that you won’t always pass technical phone interviews, but the more you do them, the better you will get. And, if you learned something from the interview, even if it was something small, it was probably worth your time.

One final tip

Always remember to thank your interviewer via email preferably by the end of the same business day that you interviewed with them. Even if the company isn’t your top choice, someone took time out of their busy schedule to interview you, so it’s important to thank them. And, if you learned something new, a quick thank you email is a great way to reiterate that.

What has your experience been like with technical phone interviews? Do you love them? Do you hate them? What has been the most interesting problem that you’ve been asked to solve? Leave a comment below or let me know by emailing me at jane [at ] fullstackinterviewing [dot ] com.

Did you like this article? Are you interested in landing your dream job in software development? Sign up for my mailing list.