Deploying a technical interview

If you want to get a job as a sorcerer-programmer, you will have to pass an interview with writing code on a piece of paper. All engineers periodically go through them – sometimes as a morning ritual, along with the careful placement of the terminal windows according to the astral plane, compulsive command execution ls in all folders (just in case something has changed overnight). With the same feelings, others are digging in the far drawer in the kitchen, where all sorts of screwdrivers, orphan pipes, and other plastic products lie – white crows among household accessories, whose original purpose has long been forgotten (or perhaps never was known), but about which we still have to care.

Today we will discuss one of the typical questions in such an interview – expand the linked list.

The first thing you need is a list. You clear the workspace of unnecessary terminal windows, pour salt in the form of two protective brackets and go into recursion, invoking a list from the abyss:

(defn cons [h t] $(if % h t))

“This is not a list,” says the interviewee. “This is a conditional statement.”

“And what are the lists,” you answer with flashing eyes, “if not alternatives?”

user=> (def x (cons 1 (cons 2 nil)))
user=> (x true)
user=> ((x false) true)

“What is x?” The interviewee tries his best to appear friendly. Show him the answer in the REPL, but don’t forget for a second: he is not your friend. Upon entering the office, you made an oath that forbids this.

user=> x
#object[user$cons$cell__4431 0x3b89cc1c "user$cons$cell__4431@3b89cc1c"]

“To know an entity, it needs to be given a name,” you say. True names are powerful. The K language, invented by Ursula K. Le Guin, is one of the oldest and most capacious forms of magic. To endow the language with the letter of your own name is to leave a piece of your soul in it. Your own initials pop up in your mind.

“Ummm, okay, how do you get an item on this list?”

A beautiful image in your imagination unfolds like a red carpet under your bare feet. Yesterday the Oscars were shown on TV, but you long for the kisses of other stars on your skin – just like that night on the Norwegian island of Sørøya, when you called the moon your mistress. Except for checking the boundaries, it turns out the first time:

(defn nth [l n]
  (when l (if (= 0 n)
            (l true)
            (recur (l false) (dec n)))))

“Can you show, well, a regular list? Like in Python?”

You grit your teeth, put your feet on the floor, and pull your autoformatter out of the abyss. You have calluses on your hands, and your eyelids are strewn with soot-black crystals-snowflakes. Any action comes at a high cost – except, of course, pure functions that have no side effects.

(defn prn-list [l]
  (print "(")
  (loop [l l]
    (if (nil? l)
      (print ")n")
      (do (print (l true))
          (when (l false)
            (print " "))
          (recur (l false))))))

No time for meaningful variable names, examples and comments. This is an interview, here time is worth its weight in gold. You remember your grandmother, who programmed in Haskell, and everything from her to you passed on… You are her continuation!

user=> (prn-list (cons 1 (cons 2 (cons 3 nil))))
(1 2 3)

The interviewee smiles reassuringly. You are now on a familiar land (or rather, hovering over it). “Now, to unfold it …”

You grab his palms. The gears in his head spin furiously, his heart pounding and torn from his chest at the sight of a spell snaking from under your cursor in an ancient language:

(defn reverse [l]
  (loop [r nil, l l]
    (if l
      (recur (cons (l true) r) (l false))

user=> (prn-list (reverse (cons 1 (cons 2 (cons 3 nil)))))
(3 2 1)

When he breaks free, he mumbles something polite and zips up his sweatshirt to keep warm. There will be other interviews, but you do not need to attend. Send an eagle instead.

They, of course, will refuse you, shamefacedly explaining this by cultural differences. Fly through the window. This office still couldn’t accommodate you.

Similar Posts

Leave a Reply Cancel reply