DEV Community

Cover image for File System Javascript Question
shrey vijayvargiya
shrey vijayvargiya

Posted on

File System Javascript Question

Google interview question — It’s simple yet tricky one to solve

Under the Hood

At the time of writing this which is today 28 March 2023, I was interviewing with the first technical round.

The question asked was based on File System, given the array of files path you have to create a file system out of it.

I’ve missed the basic thing to move the pointer or let’s say I can’t think of it at that time and missed the chance of solving the question. But the interviewer was very nice and told me that he have missed this question in the google interview.

Well, that is not comprehensible to me, I still get upset and he suggests I just sleep and relax after the interview.

Well, the question asked was not easy but doable. I can think of a couple of solutions but still, I was not able to solve them in the interview.

My Best Interview

Every time I interview I learned something new and faster than by reading articles.

I mean I always look forward to some interesting interviews where I’ve been thrown interesting questions.

4 years ago I got rejected from 42 companies straight in one month because I wasn’t know anything about programming.

But now I give interviews to test my knowledge base and to learn something new and interesting.

Let me share an interesting story from my INTERVIEW.

Last year in June 2022, I was interviewing one of the founders of a US-based startup, he has 15 years of experience and the interview was scheduled for 45 minutes but we perpetuate it to 3 hours straight.

It wasn’t the interview, to be honest, it feels like a general deep conversation about startups, ideas and technology and so on.

Although the founder rejected me and that was expected.

Here begins this story, during the interview, the interviewer asked me about my projects.

I showed him my website iHateReading and the idea of custom repositories. We discussed a lot about the website, competitors and ideas for iHateReading.

And that discussion took more than 2 hours, he end up motivating me to take my IDEA to a more specific or niche domain and try to either solve the problem or add value for the user.

I am sure this would have never happened to anyone in the world, where you went for the interview and suddenly you ended up discussing your own IDEA.

It was June last year and I was about to take the JOB but that one interview push me to think more and take my time and invest in my website or my business.

This wasn’t the interview it was a consultant meeting with the experience founder helping me or guiding me to grow fast and better.

That is why I always say this we need to open interviews a lot around discussions and thought processes and approaches.

We have chat GPT and it can easily write code for us now, so what makes a developer a developer is creativity and the approach of solving problems in multiple ways or in one creative way.

Interview Question

The question asked was simple, given the following array of files you have to create the file system out of it.

const files = [
  "/usr/lib/keytool",
  "/usr/libr/bash_profile",
  "/Downloads/shrey.txt",
]
Enter fullscreen mode Exit fullscreen mode

Our task is to create a file system from the array of files.

The answer is simple you need to create the JSON output from iterating over the array of files in the format described below.

const root = {
  "": {
      "usr" : {
        "lib": { 
           "keytool": {}
       }   
     }
  }
}
Enter fullscreen mode Exit fullscreen mode

Since “keytool” is the file so it should have content instead of being an object.

You will iterate over the array of files and split the paths into directories. Then lastly have an iteration pointer that moves to each child of the directories and appends that accordingly in the root file system.

const root = {};
for(let i=0; i < files.length; i++){
  let itr = root;
  const directories = files.split("/");
  directories.forEach(item => {
    root[item] = item;
    itr = item;
   });
};
console.log(root)
Enter fullscreen mode Exit fullscreen mode

I can’t solve it to be honest because I missed the pointer thing where I assign itr to the current directory and move forward by appending them to the root directory as the new key.

But that is fine, I’ve learned something new and will improve, the interviewer was very kind telling me that it’s okay because he himself got stumbled the first time when this same question was asked to him in an interview and that was too Google interview.

But honestly, as the developer, I feel bad and sad that why I am not able to solve the question. We tend to feel bad if we want to strive for the best it makes no sense to be able to understand and then solve the problem and called ourselves experienced developers.

Reality Check

That is where interviews are good and play the role they are literally meant for.

Interviews should be a reality check given that we don’t know this and that, and Yes there is n number of things that we can know prior to interviews but we can try and give our best.

I was prepared for n number of things to be asked but wasn’t prepared for this one file system question. This came out of the box and I was not able to solve it and that makes me feel bad and question my capability.

I immediately decided to learn about pointers and references and iterations over JSON objects. This should polish my ability to solve problem statements in minutes using solid data structure principles.

That is all I can do, to be honest, and that is all we all can do in the end.

Just accept the mistakes and learn and move forward, tomorrow I’ve one more interview, let’s see how this will go, it will be completely different than this one but hoping for the best.

Conclusion
Looking for a new opportunity, here is the website to search the job portals around the world. Link

Until next time, have a good day, people and don’t forget to subscribe.

Shrey

iHateReading

Top comments (0)