JavaScript interview with a Google engineer




Check out the feedback by the Google interviewer and the full transcript on https://interviewing.io/recordings/Javascript-Google-3/

This is a recording of a mock interview in JavaScript by a senior Google engineer on https://interviewing.io. Interviewing.io offers senior engineers free, anonymous technical interview practice with engineers from Facebook, Google, and more.

Disclaimer: All interviews conducted on interviewing.io are anonymous. In this case, we got explicit permission from the interviewer and interviewee to share the recording publicly. Interviewing.io has the sole right to distribute the content.

Original source


33 responses to “JavaScript interview with a Google engineer”

  1. Doesn't the average use-case of this algorithm merit some consideration in terms of the cost of O(n^2) versus the cost of time spent optimizing? 😉 What kind of strings would ever be so long?
    var s1 = "<string 1>";
    var s2 = "<string 2>";
    var currentInd = 0, termInd = s1.length;
    var sol = "";

    for (currentInd; currentInd<s1.length; currentInd++) {
    for (var sub = termInd; sub > 0; sub–){
    if (s2.includes(s1.substring(currentInd, sub)) &&
    s1.substring(currentInd, sub).length > sol.length) {
    sol = s1.substring(currentInd, sub);
    }
    termInd=-1;
    }
    termInd=s1.length;
    }

    console.log('Solution: ' + sol);

  2. function A (s1,s2){

    let result = {}

    let s2Array = […s2]

    let s1Array = […s1]

    s1Array.map((res,i)=>{

    s2Array.map((res2,j)=>{

    if(res === res2){

    result[i] = res2

    }

    })

    })

    return result;

    }

    i solved this in 10m

  3. Much simpler one

    const str1 = 'ABACBDAB'
    const str2 = 'BDCADB'

    function subSequence(str1, str2) {
    const s1 = str1.split('');
    const s2 = str2.split('');
    const obj = {}
    const result = [];

    s1.map(a => {
    obj[a] = true
    })

    s2.map(a => {
    if (obj[a]) {
    if (!result.includes(a))
    result.push(a)
    }
    })

    return result.join();
    }

    console.log(subSequence(str1, str2))

  4. Too common of a question, so you end up either with someone who has seen it and knows at least about the recursion solution, or someone who hasn't done a basic study loop and will in no way possible just think of the solution on the fly.

  5. I would have solved it just in simple logic that goes in my head
    Start with first Array and and than check position of each element in 2nd for example take ABAZDC and BACBAD
    Character in First = {Positions (Index)in 2nd}
    A = {1,4}
    B = {0,3}
    A = {1,4}
    Z ={}
    D = {5}
    C = {2}
    Now I have (1,4,0,3,1,4,5,2) elements picked up no delete which does not fit in increasing pattern . skip duplicate and unique increasing pattern.
    ist pattern (1,4,5)
    second (1,3,4,5) . These index in second are = ABAD

    ————————
    2nd Example
    A={4}
    G={0}
    G={0}
    T={2}
    A={4}
    B={5}
    Now I have (4,0,0,2,4,5) skip duplicate and find increasing pattern
    ist = 4,5
    2nd = (0,2,4,5) = GTAB

    ———————–
    a ={0,1}
    a={0,1}
    a={0,1}
    a={0,1}
    (0,1,0,1,0,1,0,1) skip duplicate and find increasing pattern
    first (0,1) = aa

  6. In my second year of college for CS, and I been stressing a lot lately need some tips. So I had a lab of 10 problems and I did all of them except #9 which had 3 parts, I had 2 parts working and the 3rd wasn’t fully working. I went to the TA’s which weren’t much help lol. Ever since then I felt like I don’t know if I want to continue doing this, but that might because of how much stress I was under that week from all my classes plus this lab. I talked to my teacher about this and he said I’m doing better than about 70% of the class, even though that’s good I still been feeling super stressed out and not sure if this major is even for me anymore. Lol any suggestions or tips? Sorry for the long comment

  7. function subsequence(s1,s2){
    let subS="";
    let tmp="";
    let idx=0;
    let start=0;
    for(start;start<s1.length;){
    for(let i=start;i<s1.length;i++){
    for(idx;idx<s2.length;idx++){
    if(s1[i]==s2[j]){
    tmp.concat(s1[i]);
    break;
    }
    }
    }
    idx=0;
    if(tmp.length>subS.length){
    subS=tmp;
    }
    tmp="";
    start++;
    }
    }

    Could work but its complexity is like n^3 lol

  8. I didn't understand the question until like i read the comments lmao, but took 5 mins to code it. Just copied s1 with let, mapped over s2 and if a letter occured in s1 return true, then cut s1 till after that letter(so that you know you are always moving forward with the letter check), then just turn s2 array into a string

  9. This isn't a "javascript interview". It's a problem solving exercise that lets them evaluate how you think, how you approach things, how well you adapt when your initial assumptions fail hard etc etc. Hence the beginning question: "what language do you want to do use?" They could give a shit if its java or C++ or js.

  10. it's like they don't learn how to use dynamic programming any more … yeah why should you learn the basics if your future employer needs just someone to turn that angular app into a vue one … right?

  11. The shortest possible solution I can think of

    function checker(s1,s2){

    const str = [];

    const arrS1 = […s1];

    const arrS2 = […s2];

    for(let i = 0; i < arrS1.length; i++){

    if(arrS2.indexOf(arrS1[i]) > -1){

    arrS2.splice(0,1);

    str.push(arrS1[i]);

    }

    }

    return str.join('');

    }console.log(checker("ABAZDC","BACBAD"))

  12. I think I solved this

    // "ABAZDC", "BACBAD" => "ABAD"

    // "AGGTAB", "GXTXAYB" => "GTAB"

    // "AA", "AAAA" => "AA"

    // "ABC", "XYZ" => ""

    function go(a, b) {

    if (a === "" || b === "") {

    return ""

    }

    if (b.length > a.length) {

    const c = a

    a = b

    b = c

    }

    function getNextMatchingChar(x, y, pattern) {

    if (!x.length || !y.length) {

    return pattern

    }

    for (let i = 0; i < x.length; i++) {

    const char1 = x[i]

    for (let j = 0; j < y.length; j++) {

    const char2 = y[j]

    if (char1 === char2) {

    const newX = x.splice(i + 1)

    const newY = y.splice(j + 1)

    return getNextMatchingChar(newX, newY, pattern + char1)

    }

    }

    }

    return pattern

    }

    const res = getNextMatchingChar(a.split(''), b.split(''), '')

    console.log('res', res)

    }

    go("ABAZDC", "BACBAD")

  13. Tried it

    function getLongestSubseq(s1,s2) {
    let result = "";
    let start = 0;
    if(s1.length === 0 || s2.length === 0) {
    return result;
    }
    for(let i = 0; i < s1.length; i++){
    j = start;
    while(j< s2.length) {
    if(s1[i] === s2[j]){
    result = result + s2[j];
    start = j + 1;
    break;
    }
    j++;
    }
    }
    return result;
    }
    function LongestSubseq(s1,s2) {
    const first = getLongestSubseq(s1,s2);
    const second = getLongestSubseq(s2,s1);
    if(first.length > second.length) {
    return first;
    } else {
    return second;
    }
    }
    console.log(LongestSubseq("BACBAD","ABAZDC"));
    console.log(LongestSubseq("GXTXAYB","AGGTAB"));
    console.log(LongestSubseq("","AGGTAB"));
    console.log(LongestSubseq("AGGTAB", ""));
    console.log(LongestSubseq("aaaa","aa"));
    console.log(LongestSubseq("aa","aaaa"));

  14. Here is my solution: https://github.com/Quoteme/Interviews/blob/master/js/longestSubstring.js

    “`
    const cs = (x,y) => {
    if(!x.length || !y.length)
    return "";
    if(y.indexOf(x[0])!=-1)
    return x[0] + cs( x.slice(1), y.length>0? y.slice(y.indexOf(x[0])+1):"" );
    else
    return cs( x.slice(1), y )
    }

    const lcs = (x,y) => Array.from(x)
    .map( (e,i) => x.slice( i ) )
    .map( e => cs( e,y ) )
    .reduce( (a,c) => a.length>c.length?a:c , "" )
    “`
    just pass your strings into lcs (_l_ongest _c_ommon _s_ubsequence) and you will get the right answer.
    I used only 12 lines of code and finished in ~45 minutes (but it should have been less, if only i didnt need to set up node.js)

  15. My solution: https://gist.github.com/marktellez/7c667755dadc25815fa03f56bdc683a1

    I have been coding since 1997. ( codementor.io/marktellez ) These coding "challenges" are a waste of time and do little to show the intelligence of the coder nor do they ever come up in "real world" coding…. I have written trading algorithms, blockchain tech, gambling software, RNA sequencing libraries, music sequencing, video and text based games, and countless projects to do with social networks.

    A much better challenge would be to throw someone into a pull request for a real bug and see how they dig through a codebase, read code, and come up with a way to write a test for the solution to the bug. This is just a jerk off fest.

  16. function proc(a,b,aLim) {

    let l = [];

    let bLim = 0;

    for(let i=aLim; i<a.length; i++) {

    for(j=bLim; j<b.length; j++) {

    if(a[i] == b[j]) {

    bLim = j+1;

    l.push(a[i]);

    break;

    }

    }

    }

    return l.join("");

    }

    function longest(a,b) {

    let strAr = [];

    for(let i=0; i<a.length; i++) {

    strAr.push(proc(a,b,i));

    }

    return strAr.reduce((acc='', val) => (val.length < acc.length) ? acc : val);

    }

  17. I solved this problem a while ago in LeetCode. I solved it by myself without using Google. It took around 2-3 hours, but I did it. I consider myself a decent engineer.

    Here's the deal. The thing with these types of interviews is that you have a time limit. Also, someone is pushing you not only to solve it but also to solve it efficiently. You need to perfectly and casually explain your answer and reasoning and already have a better solution if possible and have a regular chit-chat with that guy like if it was nothing special. Also, it is most likely that you won't even have a text editor.

    If you pass through that struggle, you need to repeat that for another 5-6 rounds of the same hell (depending on the company). If one interviewer doesn't like you for whatever reason, it's over, even if you solved the problem. Once you are done with the tech interview, there are also behavioral interviews.

    From my experience (interviewing at Google and Microsoft), the interviewer will rarely help you, if at all.

    My friends who work at any of the big four prepared themselves for a year and repeated the process several times. This interview is "easy mode" compared to the real thing.

Leave a Reply