Interview question: Recreate groups

I was in an interview and another interview walked in and said “Hi, do you know the groups tool in unix?” At which point i said “Yes”. At which point the interview hands me a whiteboard marker and says “Great. Write it”. And after a couple of minutes of thinking about how to do it, I wrote it down.

Now, I didn't end up doing it on a whiteboard, I did it on a piece of paper I had, which I kept, but unfortunately I have not beed able to scan it for everyone's reading enjoyment. But I'll do that in the future.

So once I got home after the interview, I took the algorithm, and applied proper PERL syntax. And here is the code:

  1. #!/usr/bin/perl
  2. #######################################################################
  3. # Created By: Bryce Verdier
  4. # on 1/19/10
  5. # Function: rework the output of the unix groups command
  6. # NOTE: FOR USE ON UNIX MACHINES!
  7. #######################################################################
  8. use warnings;
  9. use strict;
  10. use Getopt::Std;
  11.  
  12. our($opt_u);
  13. getopts('u:');
  14.  
  15. #Get the results from /etc/groups
  16. my $groups= `grep -w $opt_u /etc/group`;
  17.  
  18. #split it into an array for per line processing
  19. my @split_groups= split(/\n/, $groups);
  20.  
  21. #output the username we're looking for ( to emulate groups better)
  22. print ("$opt_u : ");
  23.  
  24. #where all the magic really happens
  25. #split the array again, and output the first part of the split, as based
  26. #on the strcuture of the /etc/groups file
  27. foreach (@split_groups){
  28. my @temp_split= split(/:/, $_);
  29. print ("$temp_split[0] ");
  30. }
  31.  
  32. #clean up
  33. print ("\n");

So after writing the code above originally, the interviewer looks things over and asks me a couple of questions. I don't remember all of them, the only one I can remember now ( almost two weeks later ) is below:

“Why are you using calling out instead of using a system call?”
After looking around, PERL's systems calls abilities are fairly minimal. I could open a file, and parse through it. But I figured why not save myself some of the work and use the tools in the shell to ease my work.

Regardless, if you are in preparing for an interview maybe this might give you a leg up. Or at least now other interviewers will have to start using a different question.

Hey Dave, Thanks for posting

Hey Dave,

Thanks for posting again!

In a way you are right, my code is not an exact duplicate of the standard "groups" command. And while the program is not exact, it does in fact display the default group... just as its first entry.

Now I admit that my test system for this is my own machine, so my testing environment is limited. (standard fedora 12 install with one user account added [my own]).

Here is an excerpt from my /etc/groups file:
root:x:0:root
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin,admn
disk:x:6:root
wheel:x:10:root,bryce
bryce:x:500:

so when I run groups bryce and root here is my output:
bryce : bryce wheel
root : root bin daemon sys adm disk wheel

But when I run my own code here is my output:
bryce : wheel bryce
root : root bin daemon sys adm disk wheel

So after looking a little more in depth at the results of the code, I can see the difference between my program and the standard tool. My program's output is based on the order of the groups file, the standard tool is not.

Again Dave, thanks for the comment.

Actually, that's not what I

Actually, that's not what I was talking about. Maybe this will be more illustrative:

[dave@hostname ~]$ grep dave /etc/passwd | awk -F: '{print $4}'
10
[dave@hostname ~]$ grep -w 10 /etc/group
wheel:x:10:root
[dave@hostname ~]$ grep dave /etc/group
apache:x:48:dave
named:x:25:dave
[dave@hostname ~]$ groups dave
dave : wheel apache named

This is on Fedora.

Cheers,

-Dave

Hey Dave, Thank you for

Hey Dave,

Thank you for posting that. I now understand the problem you were trying to tell me of before. I will modify the script to take that into account.

Thank you again Dave; I greatly appreciate it!

This does not cover their

This does not cover their default GID, which the real groups command does.

Post new comment

Google Friend Connect (leave a quick comment)
loading...
The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <geshi>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options