Skip to Content
Course content

234: Mock Coding Interview Walkthrough in JavaScript

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Alright, we've hit the home stretch. You know the syntax, you can handle the asynchronous stuff, and you've built projects. But there is a massive difference between building an app you're passionate about and solving a puzzle on a whiteboard while someone watches you breathe. Interviewing is a specific skill, and it's more about how you think than just getting the code to run.

The Mental Game: Scrambled Tiles and Baskets

Imagine I hand you a giant pile of alphabet tiles. Some are scrambled versions of the same word—like "listen" and "silent." Your job is to group all the words that use the exact same letters into their own separate baskets. If you just looked at every single word and compared it to every other word in the pile, you'd be there all day. That's what we call a "brute force" approach, and it's the first thing an interviewer wants to see you move away from.

Instead, you'd probably do this: take a word, sort the letters alphabetically (so "silent" becomes "eilnst"), and use that sorted version as a label for a basket. Now, every time you pick up a new word, you sort it. If a basket with that label already exists, you toss the word in. If not, you make a new basket with that label. This turns a chaotic search into a streamlined process of "Sort, Label, Store."

Turning the Logic into JavaScript

In a real interview, I want you to talk through that analogy before you touch the keyboard. Once the interviewer nods, you map those "baskets" to a Map and the "labels" to sorted strings. Here is how that looks in actual code.

function groupAnagrams(strs) {
    // Our 'baskets' collection
    const baskets = new Map();

    for (const str of strs) {
        // 1. Create the 'label' by sorting the string
        // We split into an array, sort it, then join it back to a string
        const label = str.split('').sort().join('');

        // 2. If the basket doesn't exist yet, initialize it with an empty array
        if (!baskets.has(label)) {
            baskets.set(label, []);
        }

        // 3. Toss the original word into its corresponding basket
        baskets.get(label).push(str);
    }

    // The interviewer usually wants an array of arrays, not the Map itself
    return Array.from(baskets.values());
}

// Testing it out:
const words = ["eat", "tea", "tan", "ate", "nat", "bat"];
console.log(groupAnagrams(words)); 
// Output: [ ["eat", "tea", "ate"], ["tan", "nat"], ["bat"] ]

Talking Through the Complexity

Once you finish the code, the interviewer is going to ask you about "Big O." Don't panic. This is where you show you aren't just guessing. I'd explain it like this: "We're iterating through the list of N strings once. Inside that loop, we sort each string. If the average length of a string is K, sorting takes O(K log K). So, the total time complexity is O(N * K log K)."

I'll let you in on a secret: interviewers often care more about you admitting a limitation than pretending the code is perfect. If you can say, "I used a sort here for simplicity, but if the character set was limited (like only lowercase English letters), I could use a frequency counter to get this down to O(N * K)," you've basically won the interview. It shows you know the trade-offs.




📋 Practical Task

The First Unique Character Hunt

It's your turn to apply the "Label and Store" mentality. Write a function called findFirstUniqueChar that takes a string and returns the index of the first character that does not repeat anywhere else in the string. If every character repeats, return -1.

Constraints:

  • The input will only contain lowercase English letters.
  • Think about how to use a Map or a plain JavaScript object to "count" occurrences before deciding which character is the first unique one.
// Expected behavior:
// findFirstUniqueChar("javascript") -> 0 ( 'j' is the first unique)
// findFirstUniqueChar("success")    -> 0 ( 's' repeats, 'u' is the first unique at index 1) 
// Wait, in "success", 's' is at 0, 5, and 6. 'u' is at index 1 and is unique. 
// So findFirstUniqueChar("success") should return 1.
// findFirstUniqueChar("aabb")       -> -1
Rating
0 0

There are no comments for now.

to be the first to leave a comment.