Common js algorithm questions in interview

  • 2021-08-05 08:57:13
  • OfStack

When we go to an interview, we usually have a written test, and when we write a test, we usually involve a lot of algorithms.

No matter how much you use it or not, it is necessary to know it anyway. Otherwise, the written test is very sad.

Even if it is a direct interview, sometimes the interviewer will directly ask you to write an algorithm on the spot.

Because the written test time is very limited, there will be no complicated questions, so the written test will not leave the following questions.

Needless to say, let's list the main algorithm problems below.

STEP 1 Sort

1 is to give an array and then sort, some from small to large, some from large to small. 1 Be sure to see clearly. The following are sorting algorithms from small to large.

Bubbling method


 var arr = [3,6,1,2,5];
 var temp;
 for(var i= 0;i<arr.length;i++){
 for(var j=i+1;j<arr.length;j++){
 if(arr[i] > arr[j]){
 temp = arr[i];
 arr[i] = arr[j];
 arr[j] = temp;
 }
 }
 }
 console.log(arr);

Quick sort method


function quicksort (arr){
 if(arr.length<=1){
 return arr;
 }
 var left = [];
 var right = [];
 var middle = arr[0];
 for(var i=1;i<arr.length;i++){
 if(arr[i]<middle){
 left.push(arr[i]);
 }else{
 right.push(arr[i]);
 }
 }
 return quicksort(left).concat([middle],quicksort(right));
}

Note: If you can use it quickly, don't use bubbling. I really don't remember to use bubbles. (Because quick sorting is designed to recursion, the interviewer wants to examine your recursion algorithm more.)

2. Array de-duplication

This question examines whether you can store the number of occurrences of array elements to solve the problem of deduplication. Of course, there are many solutions, and the following is one of them.


Array.prototype.unique = function(){
 var res = [];
 var json = {};
 for(var i = 0; i < this.length; i++){
 if(!json[this[i]]){
 res.push(this[i]);
 json[this[i]] = 1;
 }
 }
 return res;
}
var arr = [112,112,34,' How do you do ',112,112,34,' How do you do ','str','str1'];
alert(arr.unique());

3. Copy of js

This question involves whether you can clearly distinguish deep copy from shallow copy.

var a = {name:'Tom'};  var b = a;  b.name = 'Peter'; 

Excuse me, a. name =?

The correct answer is Peter. If your answer is Tom, then you should take a good look at the deep copy of js.

If you want to copy an array:

Both slice and concat can directly make deep copies of arrays


arr.slice();
arr.concat();

Here's the solution. Of course, there must be better ones than me.


function deepCopy(source){
 var result = {};
 for(var i in source){
 if(typeof source[i] === "object"){
 result[i] = deepCopy(source[i]);
 }else{
 result[i] = source[i];
 }
 }
 return result;
}

4. Get the position where substrings appear in the string


function appear(str,str_target){
 var n = 0;
 var result = [];
 while(str.indexOf(str_target,n)!=-1 && n < str.length){
 result.push(str.indexOf(str_target,n));
 n = str.indexOf(str_target,n) + str_target.length;
 }
 return result;
}
var arr = appear('abascbascbabasbascbascascbab','ab');
console.log(arr);

5. Indeterminate number of array traversal combination algorithm

All right, explain this problem. This problem will really be used in reality. Especially when doing mall website, sku algorithm is really often encountered.

The meaning of this question is to say. Equivalent to saying [1, 2, 3], [4, 5]. . . . To form [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]. Then the more arrays, the more groups will definitely come out.

That how to do, I looked up on the Internet a number of related algorithms have not found a good, and then I write it myself. There may still be something wrong with it, so everyone will make do with it.

There are better welcome comments to teach me 1.


function group(arr,re){
 if(arr.length <=0){
 return re;
 }
 if(!re){
 var arr = arr.slice();
 var re = arr.shift();
 return group(arr,re);
 }else{
 var now = arr.shift();
 var newre = [];
 for(var j=0;j<now.length;j++){
 for(var k=0;k<re.length;k++){
 var temp = [];
 if(re[k] instanceof Array){
  temp = re[k];
 }else{
  temp.push(re[k]);
 }
 newre.push(temp.concat(now[j]));
 }
 }
 return group(arr,newre);
 }
}
var arr = [['a','b','c'],['e','d','f'],['h','i'],['j','k','l','m']];
// var arr = [['a','b','c'],['e','d','f'],['h','i']];
// console.log(arr);
var result = group(arr);
console.log(result); 

6. lazyMan (this question examines a lot of contents)

Let's Baidu this problem by ourselves. . As long as Baidu lazyMan interview questions, they should be searchable

7. Write a function fn (Number n) to convert numbers to uppercase output, such as input 123 and output 12103.

Well, I forgot which article I read on this site, and I think it is really interesting.

Here's his code.


function fn(n){
 if(!/^([1-9]\d*)/.test(n)){
  return ' Illegal data ';
 }
 var unit = ' Thousand hundred 10 Billion thousand hundred 10 Thousands and hundreds 10 A ';
 if(n.length > unit.length){
  return ' Data is too long ';
 }
 var newStr = '';
 var nlength = n.length;
 unit = unit.substr(unit.length - nlength);
 for(var i = 0; i < nlength; i++){
  newStr += ' Zero 123456789'.charAt(n[i]) + unit.charAt(i);
 }
 newStr = newStr.substr(0,newStr.length-1);
 newStr = newStr.replace(/ Zero ( Thousands | Hundred |10)/g,' Zero ').replace(/( Zero )+/g,' Zero ').replace(/ Zero ( Billion | Ten thousand )/g,'$1');
 return newStr;
}
console.log(fn('205402002103'));

8. How to add commas every 3 digits to the left of a floating-point number

If 1200000.11 is changed to 12,000,000.11


result = num && num.toString().replace(/(\d)(?=(\d{3})+\.)/g,function($1,$2){
 return $2 + ',';
})

The above solution is to use regularity, of course, you can also use other methods.

These are the common interview questions. There may be omissions. Welcome to add.

Don't just look. . Write it yourself, or you think you can read it. In fact, if you want to write it, you still can't write it.

ok. That's it.


Related articles: