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
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
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.
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)
}
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)
}
For more articles www.interviewpuzzler.com
Top comments (0)