Introduction

This page contains the first part of an interactive story called Coding Interview at SimpleOS, taking the reader to a fictional coding interview. By reading this story, you will play the main character, a coder invited to a job interview and having to solve a coding challenge. While reading this story, you will decide your actions. Each action allows you to discover helpful information to complete the story. This interactive story is aiming to present to the reader coding topics in a fun and entertaining way. This story is split into four parts. Although each part can technically be done independently, I recommend to the reader to read them in order. Below are the links to all the parts. To start reading the first part click here.

Context

You are invited for a first interview at the company SimpleOS. You meet the first interviewer, Orolo, which gives you more details about the programming role you have applied to. He offers you water and coffee, and tells you that this interview aims to evaluate your coding skills. He brings you to a room where you have access to a computer (without internet connection), a coffee machine, a pen and some blank papers. On the table, you can see a paper containing the coding question. Orolo tells you that the computer is already configured with the necessary tools (editor, interpreter, compiler) and you can use all the items at your disposal to solve the coding question. He leaves you alone saying that he will come back in 90 minutes to evaluate your progress.

Your actions

Read the question

Ask a hint

Think simple

Your solution is ready

Question

Background

In SimpleOS, a folder is represented with a structure with attributes name, files and subfolders. See below a JSON representation of a simple folder containing two files and without any subfolders.

(Ref 1)

{
  "name"    : "root",
  "files"   : ['readme.txt', 'hello.txt'],
  "subfolders" : []
}

To represent a folder with subfolders, we reuse the same structure defined above in the attribute subfolders. This can represent any number of subfolder levels. See below a JSON representation of a folder containing two levels of subfolders.

(Ref 2)

{
  "name"    : "root",
  "files"   : ['readme.txt', 'hello.txt'],
  "subfolders" : [
    {
      "name"    : "videos",
      "files"   : ['vid1.mpeg', 'vid2.mpeg', 'vid3.mpeg'],
      "subfolders" : []
    }, 
    {
      "name"    : "pictures",
      "files"   : ['pic1.jpg', 'pic2.jpg'],
      "subfolders" : [
        {
          "name"    : "holidays",
          "files"   : ['h1.jpg', 'h2.jpg'],
          "subfolders" : []
        }        
      ]
    }] 
}

Your Task

Given a folder f as an input, write a JavaScript function show(f) printing on the console the folder f. See in section Output below the expected print format. The function show(f) should be able to print any folder representation regardless of the number of subfolder levels. You can assume that f is always a valid folder structure.

Output

When evaluating show(f) with (Ref 2) as its input, we obtain :

root
..readme.txt
..hello.txt
..videos
....vid1.mpeg
....vid2.mpeg
....vid3.mpeg
..pictures
....pic1.jpg
....pic2.jpg
....holidays
......h1.jpg
......h2.jpg

Back to your actions

Hint

After 20 minutes, Orolo comes back to the room to verify that you can work comfortably with the computer and asks you if you need anything. You mentioned that the computer setup is great and the question is very clear. You also indicate that you made some progress but, if possible, will be nice to get a small help to finalize your solution. Orolo states that it is possible and suggests you to read this. He then leaves you alone in the room.

Back to your actions

Think Simple

When thinking about the entire question to solve, you cannot figure out a direct solution. Instead, you decide to focus on a simpler part of the code challenge. The part you are aiming to solve now is the generation of the text to indent the folders of the various levels. After thinking a few minutes, you believe that it would be a good idea to have a function tab(t) taking an integer as an input and returning a text with the correct number of dots. As an example, the evaluation of tab(3) should return “……”.

You keep working on this idea and produce the below code.

function tab(t) {
  if (t < 1) return "" 
  return ".." + tab(t - 1)
}

Back to your actions

Solution

After 90 minutes, Orolo comes back to the room and ask you how it went. You discuss with him the question and mention how you tried to solve it. Orolo listen carefully about your code design. He says that he has with him one potential solution to the challenge. He shows it and you both start comparing it with your solution.

function show(f) {
  showCalc(f, 0)
}

function showCalc(f, i) {
  console.log(tab(i) + f.name)
  f.files.forEach(a => console.log(tab(i + 1) + a))
  f.subfolders.forEach(a => showCalc(a, i + 1))
}

function tab(t) {
  if (t < 1) return "" 
  return ".." + tab(t - 1)
}

Back to your actions

For more articles www.interviewpuzzler.com