Creative ways to do simple things

I always like exercises such as this, especially for interview questions. In an interview, I am never looking for the correct answer as often as I am how the question is answered, and how creative the person is.

For example, if you were asked to use any tool imaginable, how would you format the PATH environment variable in Unix or Linux (your choice) to each directory being printed on a newline? I can think of several ways to do this right off the top of my head, including the following:

echo $PATH | sed -e 's/:/\n/g'

…as well as…

echo $PATH | awk -F ":" '{while (i <= NF) {print $i;i++}}'

...as well as...

echo $PATH | awk '{gsub(":","\n",$0);print $0}'

...as well as....

[root@howards ~]# python
>>> import os
>>> for p in os.environ['PATH'].split(":"):
...   print p
... 
/usr/kerberos/sbin
/usr/kerberos/bin
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/usr/X11R6/bin
/root/bin
>>> 

...as well as...

strings -a /proc/self/environ | grep ^PATH= | cut -f2 -d= | tr ':' '\n'

...and finally from with an Oracle database instance. In this case, I had to build the example using CLASSPATH as PATH is not exposed in the dbms_system.get_env packaged procedure (which is itself a useful nugget of information)...

SQL> set serveroutput on
SQL> declare
  2    path varchar2(4000);
  3  begin
  4    dbms_system.get_env('CLASSPATH',path);
  5    while instr(path,':') > 0 loop
  6      dbms_output.put_line(substr(path,1,instr(path,':') - 1));
  7      path := substr(path,instr(path,':') + 1);
  8    end loop;
  9    dbms_output.put_line(path);
 10  end;
 11  /
/u01/app/oracle/product/11.2.0/dbhome_1/jdbc/lib/classes12.jar
/u01/app/oracle/product/11.2.0/dbhome_1/jdbc/lib/ons.jar
/home/oracle/java
.

PL/SQL procedure successfully completed.

SQL> 

I am sure there are a thousand others, but the point is how creative the person is, as well as how clearly they communicate. If they can give me:

* three ways to answer it
* in less than three minutes
* clearly communicate each method

...that works for me.

It doesn't mean they aren't valuable if they can't do what is above, but if they can it is confirmation they probably have a background wide enough to provide a lot of value.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.