Thread View: comp.lang.c.moderated
18 messages
18 total messages
Started by Lawrence Kirby
Thu, 29 Feb 1996 00:29
Good C Interview questions?
Author: Lawrence Kirby
Date: Thu, 29 Feb 1996 00:29
Date: Thu, 29 Feb 1996 00:29
71 lines
2915 bytes
2915 bytes
In article <4gsabh$42o@solutions.solon.com>
schwarz@a3.complang.tuwien.ac.at "Konrad Schwarz" writes:
> In article <4ggbns$86s@solutions.solon.com>, Lawrence Kirby writes:
> |> This could be better written as:
> |>
> |> static const char upmap[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
> |>
>
> or even better as
>
> static const char upmap [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
As others have noted the 26 was there for good reason. Probably the best
approach would be:
#define UCCHARS 26
static const char upmap[UCCHARS] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
> Don't I just love to quibble!
>
> but seriously: ``let your compiler do the walking''
>
> The original posting also had code of the form
> "ABC...XYZ" [ expr % 26 ]
>
> by introducing upmap, we can now write
>
> upmap [ expr % (sizeof upmap / sizeof *upmap) ]
Looks unnecessarrily complex and messy in comparison to me (and as others have
noted incorrect when used in conjunction with your change above).
upmap[expr % UCCHARS]
> which is good for two reasons:
> if we later change the length of the string, nothing breaks
The code may be self-consistent but that doesn't guarantee that it does the
right thing. The fact that there are 26 characters may be important elsewhere
and in that case it is helpful to indicate that value explicitly. However
it is still easy to change that value. upmap is then guaranteed to be the
corret size and some forms of faulty initialisation data (i.e. too much)
can be diagnosed at dompile time.
> if we later change the type of upmap, nothing breaks
Same in this case.
> Change is localized; no additional identifiers are introduced.
Where additional identifiers can simplify and clarify the code I am all
for them.
> Konrad Schwarz
>
> PS: The parenthesis are necessary above because multiplicative operators
> associate from left to right. Many people are unhappy with C's operators
> because the precedence table is long; do you think think that associativity
> is a problem too?
The problem with C's operator precedence is that some of the operators
were badly placed. The associativity rules seem OK though.
--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Re: Good C Interview questions?
Author: Ari Lukumies
Date: Fri, 01 Mar 1996 15:33
Date: Fri, 01 Mar 1996 15:33
11 lines
655 bytes
655 bytes
Good C interview questions? Consider asking what this function does and why:
char xxx(int n)
{
return "0123456789ABCDEF"[n];
}
Later,
AriL
--
All my opinions are mine and mine alone.
Re: Good C Interview questions?
Author: Andrew Snyder
Date: Fri, 01 Mar 1996 23:33
Date: Fri, 01 Mar 1996 23:33
18 lines
888 bytes
888 bytes
Ari Lukumies <aril@cmt.lpr.mail.carel.fi> wrote:
>Good C interview questions? Consider asking what this function does and why:
>char xxx(int n)
>{
> return "0123456789ABCDEF"[n];
>}
>Later,
> AriL
>--
>All my opinions are mine and mine alone.
consider n > 15
Andrew Snyder
a1s@ix.netcom.com
char disclaimer[] = {'\0'}; /* I'm on cash net */
Re: Good C Interview questions?
Author: Miguel Carrasquer Vidal
Date: Mon, 04 Mar 1996 04:15
Date: Mon, 04 Mar 1996 04:15
28 lines
1223 bytes
1223 bytes
a1s@ix.netcom.com (Andrew Snyder) wrote:
>Ari Lukumies <aril@cmt.lpr.mail.carel.fi> wrote:
>>Good C interview questions? Consider asking what this function does and why:
>>char xxx(int n)
>>{
>> return "0123456789ABCDEF"[n];
>>}
>consider n > 15
consider n < 0
Interview question: a) what does this function do and why?
b) rewrite the function to handle arbitrary input.
[The correct answer to b) is, of course, "what should I return for
values out of range?" -mod]
==
Miguel Carrasquer Vidal ~ ~
Amsterdam _____________ ~ ~
mcv@pi.net |_____________|||
========================== Ce .sig n'est pas une .cig
Re: Good C Interview questions?
Author: Chris Sonnack
Date: Fri, 08 Mar 1996 23:03
Date: Fri, 08 Mar 1996 23:03
28 lines
1132 bytes
1132 bytes
Ari Lukumies (aril@cmt.lpr.mail.carel.fi) wrote:
> char xxx(int n)
> {
> return "0123456789ABCDEF"[n];
> }
My personal favorite:
Given:
char* A = "hello" ;
int i = 3 ;
It's obvious that:
A [i] == 'l'
But what is:
i [A] == ???
Any competent C programmer should know.
--
Chris Sonnack <cjsonnack@mmm.com> http://eishcq.mmm.com
Engineering Information Services/Information Technology/3M, St.Paul, Minn
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
An intellectual is someone whose mind watches itself
Re: Good C Interview questions?
Author: Marc Espie
Date: Sun, 10 Mar 1996 16:33
Date: Sun, 10 Mar 1996 16:33
34 lines
1570 bytes
1570 bytes
In article <4hqebv$1k1@solutions.solon.com>, Chris Sonnack <cjsonnack@mmm.com> wrote: >My personal favorite: > >Given: > char* A = "hello" ; > int i = 3 ; > >It's obvious that: > > A [i] == 'l' > >But what is: > > i [A] == ??? >Any competent C programmer should know. Any *competent* C programmer should know ??? Come on, have you *ever* seen this weird construct used outside of the IOCCC ? (Maybe this is a serious question... As for me, I don't remember having ever seen it elsewhere). What is its incidence on reality, as defined by programs written to accomplish something and not to obfuscate ? Knowing the symetry of C arrays is fun, but there are loads and loads of more useful things that a C programmer should know first and foremost. Why not consider trigraph knowledge a must as well ? -- [nosave]<http://www.eleves.ens.fr:8080/home/espie/index.html> microsoft network is EXPLICITLY forbidden to redistribute this message. `Moon purismu powa, make up.... Tsuki ni kawatte, oshiokiyo !' Marc Espie (Marc.Espie@ens.fr)
Re: Good C Interview questions?
Author: John Carr
Date: Mon, 11 Mar 1996 01:29
Date: Mon, 11 Mar 1996 01:29
35 lines
1955 bytes
1955 bytes
In article <4hv081$jdt@solutions.solon.com>,
Marc Espie <espie@clipper.ens.fr> wrote:
>>It's obvious that:
>> A [i] == 'l'
>>But what is:
>> i [A] == ???
>
>>Any competent C programmer should know.
>
>Any *competent* C programmer should know ??? Come on, have you *ever*
>seen this weird construct used outside of the IOCCC ?
I've used it once. In the process I discovered that IBM's xlc compiler
doesn't like reverse array notation -- it generates bad code.
I think a C programmer should know that array indexing is commutative,
but I wouldn't ask about it in an interview. Unless I was interviewing
somebody just out of school for a first real programming job I wouldn't
ask detailed C syntax/semantic questions at all. There are too many
details of C. It would take a long time to ask about them all, and it
is reasonable not to know some of them.
Q: Which has higher precedence, + or |?
A: I don't remember, so I use parentheses.
I really didn't remember and I've been using C for more than 10 years.
When I need to know I take 30 seconds to write a C program to see what the
compiler does, or read the standard, or look at gcc's parser. (Sun's C
compiler says + is higher than |.) Maybe that would be a good interview
question: ask what the precedence is and if the answer is "I don't know"
follow up by asking: "show me how you would find out".
--
John Carr (jfc@mit.edu)
Re: Good C Interview questions?
Author: Eric Goebelbecker
Date: Tue, 12 Mar 1996 01:27
Date: Tue, 12 Mar 1996 01:27
29 lines
1939 bytes
1939 bytes
John Carr wrote: > (snip discussion on reverse array notation) > > Q: Which has higher precedence, + or |? > A: I don't remember, so I use parentheses. > > I really didn't remember and I've been using C for more than 10 years. > When I need to know I take 30 seconds to write a C program to see what the > compiler does, or read the standard, or look at gcc's parser. (Sun's C > compiler says + is higher than |.) Maybe that would be a good interview > question: ask what the precedence is and if the answer is "I don't know" > follow up by asking: "show me how you would find out". > > -- > John Carr (jfc@mit.edu) I agree completely! Nobody can remember everything, but knowing how to answer your own questions is probably one of the most important skills a prospective employee can have. (One of the others is knowing what to ask.) -- ----------------------------------------------------------- | Eric Goebelbecker | | | Market Vision | You are a strange, | | 40 Rector Street | sick, little man." | | New York, NY. 10006 | | | phone: (212)306-0355 | - Buzz Lightyear | -----------------------------------------------------------
Re: Good C Interview questions?
Author: Dale Johannesen
Date: Tue, 12 Mar 1996 01:27
Date: Tue, 12 Mar 1996 01:27
32 lines
1622 bytes
1622 bytes
In article <4hv081$jdt@solutions.solon.com>, espie@clipper.ens.fr (Marc Espie) writes: |> In article <4hqebv$1k1@solutions.solon.com>, |> Chris Sonnack <cjsonnack@mmm.com> wrote: |> >My personal favorite: |> > |> >Given: |> > char* A = "hello" ; |> > int i = 3 ; |> > |> >It's obvious that: |> > |> > A [i] == 'l' |> > |> >But what is: |> > |> > i [A] == ??? |> |> >Any competent C programmer should know. |> |> Any *competent* C programmer should know ??? Come on, have you *ever* |> seen this weird construct used outside of the IOCCC ? |> (Maybe this is a serious question... As for me, I don't remember having |> ever seen it elsewhere). It also appears in test suites for compilers. IMO your overall point is well taken, however. |> What is its incidence on reality, as defined by programs written to accomplish |> something and not to obfuscate ? Knowing the symetry of C arrays is fun, |> but there are loads and loads of more useful things that a C programmer |> should know first and foremost. Why not consider trigraph knowledge a must |> as well ?
Re: Good C Interview questions?
Author: Kevin Miller
Date: Wed, 13 Mar 1996 12:39
Date: Wed, 13 Mar 1996 12:39
52 lines
2909 bytes
2909 bytes
jfc@mit.edu (John Carr) writes:
>In article <4hv081$jdt@solutions.solon.com>,
>Marc Espie <espie@clipper.ens.fr> wrote:
>>>It's obvious that:
>>> A [i] == 'l'
>>>But what is:
>>> i [A] == ???
>>
>>>Any competent C programmer should know.
>>
>>Any *competent* C programmer should know ??? Come on, have you *ever*
>>seen this weird construct used outside of the IOCCC ?
Does IOCCC stand for "International Organization of Competent C Coders"?
Since A[i] is turned into *(A+i), I was able to figure out on my own
that if the second expression at the top of this post compiled, then it
would have to produce the same result as the first one. However, I would
have thought that the compiler would do a check whenever it saw brackets
to make sure that the thing before the brackets was a pointer and the
thing inside the brackets was an integer. However, K&R state that
indexing is commutative, and a quick test program compiled and run
using my (admittedly substandard) compiler confirms this. So I learned
something new today! :)
>I've used it once. In the process I discovered that IBM's xlc compiler
>doesn't like reverse array notation -- it generates bad code.
>I think a C programmer should know that array indexing is commutative,
>but I wouldn't ask about it in an interview. Unless I was interviewing
>somebody just out of school for a first real programming job I wouldn't
>ask detailed C syntax/semantic questions at all. There are too many
>details of C. It would take a long time to ask about them all, and it
>is reasonable not to know some of them.
>Q: Which has higher precedence, + or |?
>A: I don't remember, so I use parentheses.
>I really didn't remember and I've been using C for more than 10 years.
>When I need to know I take 30 seconds to write a C program to see what the
>compiler does, or read the standard, or look at gcc's parser. (Sun's C
>compiler says + is higher than |.) Maybe that would be a good interview
>question: ask what the precedence is and if the answer is "I don't know"
>follow up by asking: "show me how you would find out".
My answer would be: I would just look on page 53 of Kernighan & Ritchie.
(Since I already know said page number by heart.)
- Kevin Jay Miller
`
Re: Good C Interview questions?
Author: MCLEOD James William
Date: Wed, 13 Mar 1996 12:39
Date: Wed, 13 Mar 1996 12:39
20 lines
1128 bytes
1128 bytes
John Carr wrote: > (snip discussion on reverse array notation) > > Q: Which has higher precedence, + or |? > A: I don't remember, so I use parentheses. > (snip comment and agreement) I have to wonder, in what context would you want to mix arithmetic and bitwise operations? It strikes me that, with modern optimizing compilers, you will save NOTHING (except clarity :-]) by doing this. My answer would be, A: Who cares? The code is either flaky or unclear. Regards James McLeod mcleod@ecf.utoronto.ca
Re: Good C Interview questions?
Author: David McReynolds
Date: Wed, 13 Mar 1996 12:40
Date: Wed, 13 Mar 1996 12:40
34 lines
1922 bytes
1922 bytes
Marc Espie (espie@clipper.ens.fr) wrote: : In article <4hqebv$1k1@solutions.solon.com>, : Chris Sonnack <cjsonnack@mmm.com> wrote: : >My personal favorite: : > : >Given: : > char* A = "hello" ; : > int i = 3 ; : > : >It's obvious that: : > : > A [i] == 'l' : > : >But what is: : > : > i [A] == ??? : : >Any competent C programmer should know. : : Any *competent* C programmer should know ??? Come on, have you *ever* snip : What is its incidence on reality, as defined by programs written to accomplish : something and not to obfuscate ? Knowing the symetry of C arrays is fun, : but there are loads and loads of more useful things that a C programmer Nope, never seen it in practice. But, I did have it drilled into my head during my advanced C class. The point is not that he knows something obfuscated but, rather, he understands what the compiler is doing with the given construct. It really has more to do with the equivalency of arrays and pointers than commutative properties. If the interviewee can answer the question then you know there is a good chance that he has discussed things like a[i][j] == *( *( a + i ) + j ). The fact that the FAQ for this group has an entire chapter devoted to such a topic simple underscores its relevance.
Re: Good C Interview questions?
Author: Michael J. Creutz
Date: Thu, 14 Mar 1996 02:30
Date: Thu, 14 Mar 1996 02:30
21 lines
998 bytes
998 bytes
>: Chris Sonnack <cjsonnack@mmm.com> wrote: >: >My personal favorite: >: > >: >Given: >: > char* A = "hello" ; >: > int i = 3 ; >: > >: >It's obvious that: >: > >: > A [i] == 'l' >: > >: >But what is: >: > >: > i [A] == ??? What's confusing about this? The "i'th element of A" is about as intuitive as you can get. Only frustrated fortran programmers would prefer the former :-). Mike
Re: Good C Interview questions?
Author: Ari Lukumies
Date: Mon, 18 Mar 1996 13:51
Date: Mon, 18 Mar 1996 13:51
30 lines
1382 bytes
1382 bytes
Michael J. Creutz wrote: > > >: Chris Sonnack <cjsonnack@mmm.com> wrote: > >: >My personal favorite: > >: > > >: >Given: > >: > char* A = "hello" ; > >: > int i = 3 ; > >: > > >: >It's obvious that: > >: > > >: > A [i] == 'l' > >: > > >: >But what is: > >: > > >: > i [A] == ??? > > What's confusing about this? The "i'th element of A" is > about as intuitive as you can get. Only frustrated fortran > programmers would prefer the former :-). > You mean that i[A] is preferred to A[i] ? I don't agree. The standard makes the two identical, but I wouldn't want to maintain code using the latter approach. Well, maybe if you were coding Lisp... Later, AriL -- All my opinions are mine and mine alone.
Re: Good C Interview questions?
Author: Ed.L.Karrels
Date: Sat, 30 Mar 1996 04:01
Date: Sat, 30 Mar 1996 04:01
53 lines
2508 bytes
2508 bytes
I've read a lot of posts in this thread about what _would_ be good
interview questions. Well, I have a few that _were_ asked of me when
I had a telephone interview with SGI a few weeks ago.
What does it mean when you declare something static?
(I supplied answers for file scope, local variables, and inside
C++ classes. Do you guys know of other situations?)
What does the comma operator do, and what does it return?
What's the best way to get the size of this structure?
struct foo {
int a;
char b;
int c;
char d;
};
(I said, "sizeof(struct foo)". He chuckled and said, "I can tell you're
a C programmer. A C++ programmer would have just said sizeof(foo)." :-))
Pick your favorite architecture. What would the sizeof(foo) be on it?
(I picked what we have at school, Sun workstations with gcc. Should have
said SGI's :-))
Why isn't the size 2*sizeof(int)+2*sizeof(char)?
How could you decrease the size of the structure without removing
any members?
Then some C++ <crap> on which I didn't as well, but you guys wouldn't
wouldn't want to hear about that...
Then some data structures stuff, like running time for quick sort, heap
sort, hash tables, and various operations on singly and double linked
lists. Then, he asks, "OK, you've correctly told me that some operations
are more efficient for doubly linked lists than for singly linked lists,
but doubly linked lists use the extra memory of a pointer at each node.
How can we get all the efficiency of a doubly linked list, but use
only as much memory as a singly linked list, assuming the full significance
of the pointers is required?
This one, I heard about once on this newsgroup. Store the bitwise XOR of the
addresses of the next and previous nodes in each node. BTW, is there
another answer you guys know of? I have some sample code that implements
this for any interested parties, email me if you want it.
--
Ed L. Karrels
karree46@sol.acs.uwosh.edu
http://www.uwosh.edu/students/karree46/
Re: Good C Interview questions?
Author: Chris Torek
Date: Sun, 31 Mar 1996 16:43
Date: Sun, 31 Mar 1996 16:43
72 lines
3076 bytes
3076 bytes
In article <4jibmd$8b5@solutions.solon.com> <Ed.L.Karrels@zippo.com> writes:
>... I have a few that _were_ asked of me [recently].... Then, he
>asks, "OK, you've correctly told me that some operations are more
>efficient for doubly linked lists than for singly linked lists,
>but doubly linked lists use the extra memory of a pointer at each node.
>How can we get all the efficiency of a doubly linked list, but use
>only as much memory as a singly linked list, assuming the full significance
>of the pointers is required?
>This one, I heard about once on this newsgroup. Store the bitwise XOR
>of the addresses of the next and previous nodes in each node.
It is probably worth pointing out that, while this trick works
quite often, it is ultimately not portable and should generally be
avoided. (Your first problem is finding an integral type long
enough to hold a pointer on every implementation. On some, there
may not be any such type, and you might even have to resort to an
array of `unsigned char'!)
Indeed, if space is *that* tight, you may be better off using an
array of elements, doing the linking with array indices. That is,
instead of:
struct elem {
struct elem *forw, *back;
...
};
void init_queue(struct elem *headp) {
headp->forw = headp->back = headp;
}
/* insert element `e' after element `after' */
void insert(struct elem *e, struct elem *after) {
e->forw = after->forw;
e->back = after;
after->forw = e;
e->forw->back = e;
}
you might have as your initial transformation:
struct elem {
unsigned int forw, back;
...
};
struct elem space[MAX_ELEM];
#define qptoix(qp) ((qp) - &space[0])
#define ixtoqp(ix) (&space[ix])
void init_queue(struct elem *headp) {
headp->forw = headp->back = qptoix(headp);
}
/* insert element `e' after element `after' */
void insert(struct elem *e, struct elem *after) {
e->forw = after->forw;
e->back = qptoix(after);
after->forw = qptoix(e);
ixtoqp(e->forw)->back = qptoix(e);
}
Now two indices *can* be xor'd portably, and the result uses the
same number of bits as the original values. If the array size is
65536 or smaller, an index can be stored in an unsigned short. On
a machine with 64-bit pointers, this could save 14 bytes per element.
(Of course, we might wonder why a machine with 64-bit pointers was
so tight on space in the first place. :-) )
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc
El Cerrito, CA Domain: torek@bsdi.com +1 510 234 3167
Re: Good C Interview questions?
Author: Bill Wendling
Date: Sun, 31 Mar 1996 16:43
Date: Sun, 31 Mar 1996 16:43
66 lines
3274 bytes
3274 bytes
Ed.L.Karrels@zippo.com inexplicably wrote:
} I've read a lot of posts in this thread about what _would_ be good
} interview questions. Well, I have a few that _were_ asked of me when
} I had a telephone interview with SGI a few weeks ago.
} What does it mean when you declare something static?
} (I supplied answers for file scope, local variables, and inside
} C++ classes. Do you guys know of other situations?)
} What does the comma operator do, and what does it return?
} What's the best way to get the size of this structure?
} struct foo {
} int a;
} char b;
} int c;
} char d;
} };
} (I said, "sizeof(struct foo)". He chuckled and said, "I can tell you're
} a C programmer. A C++ programmer would have just said sizeof(foo)." :-))
} Pick your favorite architecture. What would the sizeof(foo) be on it?
A compile error, right?
} (I picked what we have at school, Sun workstations with gcc. Should have
} said SGI's :-))
} Why isn't the size 2*sizeof(int)+2*sizeof(char)?
Byte alignment?
} How could you decrease the size of the structure without removing
} any members?
Rearrange the elements.
} Then some C++ <crap> on which I didn't as well, but you guys wouldn't
} wouldn't want to hear about that...
} Then some data structures stuff, like running time for quick sort, heap
} sort, hash tables, and various operations on singly and double linked
} lists. Then, he asks, "OK, you've correctly told me that some operations
} are more efficient for doubly linked lists than for singly linked lists,
} but doubly linked lists use the extra memory of a pointer at each node.
} How can we get all the efficiency of a doubly linked list, but use
} only as much memory as a singly linked list, assuming the full significance
} of the pointers is required?
} This one, I heard about once on this newsgroup. Store the bitwise XOR of the
} addresses of the next and previous nodes in each node. BTW, is there
} another answer you guys know of? I have some sample code that implements
} this for any interested parties, email me if you want it.
Could you maybe post it? What about it, moderator?
[I'd rather see a pointer to it posted, unless it's quite small. In no
case would I like to see a URL for this code, and the code, xor'd together
and posted. See also Torek's post about it, just recently approved. -mod]
--
Bill Wendling bwendlin@fgi.net
GCS/M/S d s-:+ a? C++++$ UL++>++++ P+++>+++++ L+++>+++++ E--- W-- N++ !o
!K w--- O M-- !V PS+++@ PE- Y !PGP t+++(*) 5-- X- R++ tv--- b+++ DI++++
!D G+ e++>+++++ h(++) !r !y+
Re: Good C Interview questions?
Author: Miguel Carrasquer Vidal
Date: Mon, 01 Apr 1996 13:20
Date: Mon, 01 Apr 1996 13:20
17 lines
1024 bytes
1024 bytes
Ed.L.Karrels@zippo.com wrote: >I've read a lot of posts in this thread about what _would_ be good >interview questions. Well, I have a few that _were_ asked of me when >I had a telephone interview with SGI a few weeks ago. [questions and answers omitted] Well, did you get the job? == Miguel Carrasquer Vidal ~ ~ Amsterdam _____________ ~ ~ mcv@pi.net |_____________||| ========================== Ce .sig n'est pas une .cig
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads