Tuesday, 23 February 2010

Questions about tcl for job interviews, thoughts on tcl quirks

Tricky question. "What is a simple question and answer that would give us confidence that they knew TCL?"

Try and set up interview so your candidate can write code. You can’t determine it with one question I think but you can if you get them to write a function. Incidentally The Gureilla Guide to Interviewing by Joel Spolsky FogCreek is very good on technical/software interviews.

One question might be what kinds of different data types are available by default in tcl?
Answer1: in tcl, “everything is a string” (but lots of people who know tcl mightn’t get this!)
Answer2: There is default support for list and array. And normal var types would be considered integers/floats/strings.
“everything is a string” means there is no type protection
Data types are supported by the way they are treated in procedures.

Write a procedure (that would require a while or for loop) to calculate something.
Show how this can be called and print the answer.
e.g. calculate the sum of a list

# my answer
proc calc_sum_of_list { list } {
set sum 0
foreach a $list {
# OR if you're a real tcler here use: incr sum $a
set sum [expr $sum + $a]
return $sum

# how this is called
set mList [list 1 2 3 4]
puts “sum of list result: [calc_sum_of_list $mList]”

# OR
set mList [list 1 2 3 4]
set result [calc_sum_of_list $mList]
puts “sum of list result: $result”

Simpler questions:

How do you set a variable?
Answer: set a 40

How do you increment an integer variable?
Answer: incr a

How do you decrement?
Answer: incr a -1

How to you print a variable?
Answer: puts $a
OR puts “a is $a”
OR puts [format “a is %d = 0x%08x, string %s” $a $a “string”]
OR ...

A fundamental tcl question: Are spaces needed in tcl in for/while/if loops?
Answer: YES tcl has a couple of really awkward quirks.

e.g. valid tcl:
set a 20
if {$a > 4} {
puts “$a is bigger than 4”

set a 20
if{$a > 4} {
puts “$a is bigger than 4”

invalid command name "if{20"

set a 20
if {$a > 4}
puts “$a is bigger than 4”

wrong # args: no script following "{$a > 4}" argument
invalid command name "
puts "$a is bigger than 4"

Another fundamental question: In tcl what brackets in comments might cause problems (especially if they’re not balanced in comments)?
Answer: Curley brackets {} cause problems. parenthesis () or square [] or anything else don’t cause problems in comments.

Pure and even very impure software people who haven't encountered tcl before have probably already WTFed several times and swooned away by now. Must be because tcl syntax was defined by one of those hardware people! :-P i.e. Possibly a very practical person who has made a very gluey language with a few glaring hairy things remaining in the syntax (possibly on purpose to scare/annoy computer science philosophers).
Please excuse me hardware and software people alike :)
Tcl language syntax overview
Why is tcl syntax so weird?on wiki.tcl.tk
syntax related pages on wiki.tcl.tk
John Ousterhout on tcl history

No comments: