Recently, some students had an interview with Tencent R&D Department and came back to communicate with me. Tencent’s interview was generally ok, but there were two penned questions, which were very messy at that time. Later, they were rejected because of these two penned questions.
After I got these two interview questions, I did not search for the answers. I had some ideas in mind and referred to some other answers after I finished them. The overall results are almost the same.
How to determine if the URL contains only QQ.com
This problem is believed to be easy at a glance, but it is not so simple, url has many forms: HTTP: / / http://www.qq.com / / by not HTTP: / / http://www.qq.com.cn / / http://www.qq.com/a/b/through/by / / through http://www.qq.com?a=1 http://www.123qq.com?a=1 // Not passed
At present, MY first glance is to use the re to judge, you can not look at my answer, see if you can handwritten re to do a verification.
function check(url){
if(/.*(\.qq\.com)($|[\?\/])/.test(url)){
return true;
}else{
return false;
} } Copy the code
The above re is simple, ending with qq.com or including? And /, but what if the parameter contains the URL qq.com? http://www.qq.com?redirect=http://www.qq.com/a
function check(url){
if(/^(http:\/\/[^=]+\.qq\.com)($|[\?\/])/.test(url)){
return true;
}else{
return false;
} } Copy the code
You can extract the domain name in front, starting with HTTP, including qq.com, but cannot contain any character in the middle =, and end with qq.com or include? And /
As a sidebar for regex, parentheses represent expressions, which, when executed with exec, search and match strings by subexpression.
/ ^ (HTTP: / / / / / ^ = + \. Qq\.com) ($| [\ \ /?])/the regular contains two expressions, so an exec, will contain three elements in the array, the default can be compatible to the first element is the regular string. The second element is the result of matching the first expression, and the third element is the result of matching the second expression.
How do I add two large numeric strings
This question also looks relatively simple, but handwritten, also take some effort, to test our thinking ability, logical thinking ability, my first instinct, is to convert into a string, respectively, the sum of the calculation, and finally output in the form of a string. Don’t even think about parseInt!!
Again, look at the code and see if you can do it!
var a = '10000000000000000000000'
var b = '456789345678945678945678'
function add(a,b)
Copy the code
Plan a
Ideas: Whether two string length, by way of filling 0, let they have the same length, because we are in the age of the students, this kind of addition and subtraction is from right to left, so we to inversion of array, so that the computer in accordance with our ideas to achieve, in the process of doing, must consider the full 10 in 1 case, all is finished, turn back again, Join to return a string.
// both a and b are strings. Add returns an additive string
// a = '12345678'
// b = '456789'
function add(a,b){
// Get the respective lengths
let i = a.length,j = b.length; // Decide who is older let len = i - j; let sum,sumArr = []; // Add 0 in front of the smaller one so that the two lengths are the same // a = '12345678' b = '00456789' if(len > 0) { b = appendZero(b,len); }else if(len<0) { // This place is going to make len positive a = appendZero(a,Math.abs(len)); // When the length of B is greater than that of A, the length of B is given to I i = j; } // Since everyday operations are done from right to left, we reverse the array let aArr = a.split(' ').reverse(), bArr = b.split(' ').reverse(); for(let m = 0; m < i; m++){ // Add two elements in the same position // If sumArr has a value, it is possible that a 1 is added to the previous 10 let c = parseInt(aArr[m])+parseInt(bArr[m]) + (sumArr[m] || 0); if(c > 9) { // carry 10, take the remainder sumArr[m] = c%10; // If the value is 10, a 1 is added to the last element of the array sumArr[m+1] = parseInt(sumArr[m+1] | |0) +1; }else{ sumArr[m] = c; } } // Finally, the array is reversed and merged into a string sum = sumArr.reverse().join(' '); return sum; } function appendZero(str,len){ for(let i=0; i<len; i++){ str='0'+str; } // Or use ES6 repeat // str = '0'.repeat(len) + str; return str; } Copy the code
This looks a little bit complicated, but it’s a pretty basic algorithm, and I was thinking about doing it from the beginning.
Scheme 2
function add(a,b){
// Get the respective lengths
a = a.split(' '), b = b.split(' ');
let sum=[],go=0;
while(a.length || b.length){
// Fetch one at a time via pop let num1 = parseInt(a.pop()) || 0; let num2 = parseInt(b.pop()) || 0; // Add the two values, + go if there is a carry let tmp = num1 + num2 + go; if(tmp > 9) { go = 1; / / modulo tmp %= 10; }else{ go = 0; } // array.unshift(item) inserts in front of array sum.unshift(tmp) } if(go) sum.unshift(1); return sum.join(' '); } Copy the code
The second solution, seemingly simpler than the first one, is to use the while loop, which is also used to complement the insufficient length of the 0, and cleverly fetch an element from the last element by pop, just like the previous array inversion.
Well, today simple to introduce two Tencent handwritten interview questions, I hope to help you.
Focus on the front end, focus on the future, I am your little help at work: the front future
This article is formatted using MDNICE