Skip to main content

Command Palette

Search for a command to run...

Coding Game | 1st coding interview with SimpleOS

Updated
4 min read
Coding Game | 1st coding interview with SimpleOS
B

Coder and gamebook reader

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.

What do you do ?

Read the question

Ask a hint

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

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.

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 "" 
    else return ".." + tab(t - 1)
  }
  

Z
zgd1y ago

We are willing to pay a high price to acquire the source code of a mobile game that is compatible with both Android and iOS platforms, for the purpose of learning and research. The game must meet the following criteria: it should be developed by a top-tier gaming company within the past 5 years, fully completed and matured, and preferably of the multiplayer online MMORPG or ARPG genre. If your game meets our interest, we will proceed with the transaction promptly and offer an upfront payment as a gesture of goodwill. Contact me q791864008q@gmail.com

Z
zgd1y ago

We are willing to pay a high price to acquire the source code of a mobile game that is compatible with both Android and iOS platforms, for the purpose of learning and research. The game must meet the following criteria: it should be developed by a top-tier gaming company within the past 5 years, fully completed and matured, and preferably of the multiplayer online MMORPG or ARPG genre. If your game meets our interest, we will proceed with the transaction promptly and offer an upfront payment as a gesture of goodwill. Contact me q791864008q@gmail.com