share
Stack OverflowUnusual interview question
[+34] [6] Andrzej Nosal
[2010-09-15 12:40:03]
[ c# .net interview-questions ]
[ http://stackoverflow.com/questions/3717636] [DELETED]

College friend told me about interview question that is asked in one company:

Write reasonable(=compilable) method declaration that contains as many C# keywords as possible.

  • If there are 2 or more the same keywords they are counted as one.

  • Method's body is not considered.

C# has finite number of keywords so such method must exist. What is the code of this declaration?

EDIT:

Edited to make it sound like real question.

(5) Had heard of code coverage. This is like keyword coverage. - codaddict
(6) "Think about X" is not a question. I don't get it. - Coronatus
(59) Sounds like a strong candidate for "worst interview question ever". And probably also a big warning sign: "DO NOT TAKE THIS JOB" - jalf
What do they mean by 'keywords'? And why would you specify one more than once? Do they mean parameters and the params "keyword"? - w69rdy
(3) I think if the interviewer does ask this question he's just way too bored. - BoltClock
What does the third part mean? They're only asking for a declaration? - SLaks
Yeah, just the declaration - Grant Crofton
(2) ok, after looking at the suggested answers, I'd like to officially nominate C# to take over C++'s role as "absurdly bloated monstrosity of a language". - jalf
(12) I'll disagree with most of the comments; it's a decent question. Do you know what virtual means? internal? static? unsafe? You can quickly judge how much someone has used C# by their answer, as long as you're willing to accept quite a few incomplete answers. It's also useful to judge how the person thinks about problems they've never heard before. - Dean J
should this kind of question moved to programmers.stackexchange.com? - nanda
@jalf: You can't do that until you look at the C++ version, with template parameters but with fewer keyword types. - SLaks
Dean: Yes, anyone with a decent amount of experience will hear "reasonable" and limit the answer to about 4 keywords. :-) - Ken
@Coronatus: +1 for attention to detail. That's the only correct answer to this "question" I'd expect from a good programmer. - Karmic Coder
In Perl they code golf, in C# they... uhhh... anti-golf? - Paul Nathan
[+90] [2010-09-15 12:49:24] SLaks

This declaration uses 38 keywords:

[method: MethodImpl(MethodImplOptions.InternalCall)]
[SomeAttribute(typeof(object))]
[return: MarshalAs(UnmanagedType.U1)]
internal static extern unsafe void MyMethod<T1, T2, T3>(
    this sbyte a, ref short b, out int c, long d, 
    byte e, ushort f, uint g, [param: MarshalAs(UnmanagedType.U4)]ulong h,
    float i, char j = default(char), bool k = false, global::System.Boolean l = true,
    double m = sizeof(int), decimal? n = null, dynamic o = null, params string[] z)
where T1 : struct where T2 : class where T3 : new();

It actually compiles, too.


(1) you miss the generics... - Yves M.
till now you're the bhest - 24 keywords ! :) - Andrzej Nosal
When you use override you cannot use contraints, my compiler tells me. - Jens
You're the man, but as someone mentioned above, one of the parameters should be = default(T). Oh, hmm, my compiler complains that you can't set a default value for a params array : ( - mquander
@Yves: indeed, one of the argument types could be something like MyGeneric<in T, out S> for an extra one or two keywords (are different uses of out different keywords?) - Richard
@mquander: Yes; I just fixed that. - SLaks
(5) You could probably get a global in there. global::System.Double - Mark H
Surely the attributes don't count as keywords though? - Grant Crofton
@Richard: No, you can't. Those can only be used in interfaces. - SLaks
@Grant - the attribute itself doesn't, but it's required for the internal to work. However, you could turn the attribute to say [method:MethodImpl(...)], though I'm not sure if method is a keyword, it's highlighted as such in VS. - Mark H
Can you fit delegate or event in as parameters or constraints? Or maybe even as accessors? - Jimmy Hoffa
Could you add T4 : enum or I'm wrong - Andrzej Nosal
@A: No, you can't. - SLaks
@Mark: You mean extern. Thanks; I'd forgotten about those. - SLaks
(3) You can have dynamic args, can you not? - Mark H
@Mark: Yes; thanks. - SLaks
Really like the use of attributes to sneak in some extra keywords, but I would question whether they are strictly part of a "method declaration" rather than metadata. Though I think we both left the spirit of the question behind long ago. - Martin Harris
@Martin: If Grant's thoughts on the question are correct, using attributes in this manner does not necessarily avoid the spirit of the question. - Brian
@SLaks: I added the protected keyword...still compiles. - Brian Gideon
(1) @Brian Gideon Are you sure? The first parameter is a 'this' which marks it as an extension method. You can't have protected methods in a static class (which it'll needs to be to declare extensions) - Martin Harris
@Martin: Doh...you're right. I didn't see that. I reverted my edit. That's pretty funny actually. The signature is so complicated now that you cannot see the trees through the forest :) - Brian Gideon
I'm weeping....and I don't know why - curtisk
(47) A truly beautiful piece of code, easy to read and maintain. - Daniel Daranas
(1) You can learn half of c# syntax reading this declaration (-: - Oren A
(10) There are many words I would use to describe this code, but "reasonable" is not one of them. - Ken
(1) I would ask the interviewer to define reasonable. - abhi
(6) The keywords get, set, value, add, remove, assembly, module, type, method, field, property, param, typevar, where, partial, global, yield, alias, hidden, default, disable, restore, checksum, from, join, on, equals, into, orderby, ascending, descending, group, by, select, let, var and dynamic are all unreserved contextual keywords. You can use them as type or parameter names in a declaration. Feel free to use any of those that you haven't already! - Eric Lippert
@Eric: They don't count. (Unless used in context, such as default, dynamic, method, parameter, and return) - SLaks
How are checksum, disable, and restore keywords? For #pragma? - SLaks
Also, do you mean params? - SLaks
The question uses a key word too, which was reasonable :). - Daniel Cassidy
@SLaks: checksum, disable and restore are not actually defined by the language but they are recognized by the compiler for the #pragma directive. "param" is an attribute target. - Eric Lippert
(1) @Eric: Why does this parameter compile: [garbage: MarshalAs(UnmanagedType.U4)]ulong h,? - SLaks
(1) @SLaks: When I compile that I get "warning: garbage is not a recognized attribute location. All attributes in this block will be ignored". Is your question why this is a warning rather than an error? - Eric Lippert
@Eric: Yes. I was testing in LINQPad, so I didn't see the warning. - SLaks
(2) @SLaks: I see no justification for why this is a warning and not an error, either in the spec, the design notes, or the code. It certainly looks like it ought to be an error. The only thing I can think of is maybe it's for some forwards compatibility situation. I'll ask one of the old timers if they remember why they did this strange thing. - Eric Lippert
(5) They don't remember, but concur that it was probably for some forwards-compat scenario. - Eric Lippert
(2) Now give us a real world example where this method would be useful/necessary ("Answering a stupid interview question" doesn't count). - DrDro
@DrDro, It unnecessarily has to be applied somwhere (though it cannot be excluded). It can be used as quick reference when you forget syntax of generic constrained extension method or to show beginner, in one example how to define method. It can be also a thought excercise which reminds you that partial and extern cannot be mixed :) - Andrzej Nosal
1
[+28] [2010-09-15 12:47:35] Martin Harris

As a thought exercise I came up with this, though I doubt it is definative:

protected internal new virtual unsafe void MethodName<T>(
    out int a, 
    ref int b,
    params int[] c) where T : class, new()

(Also not sure completely sure whether extern methods can be generic...)

Sounds like a completely nonsensical question to me though and I'm not sure what they were hoping to achieve by asking it.


Ah, looking that complete list of C# keywords [1] all of the CLR types are considered to be keywords. Therefore assume the method above takes one parameter of each type - I'll be damned if I'm writing that out though...


Okay, based on the comments (I wish I hadn't started this now...)

protected internal static extern unsafe void MethodName<T, U>(
    out byte a, 
    ref char b,
    decimal c,
    double d,
    float e,
    long f,
    sbyte g,
    short h,
    uint i,
    dynamic j,
    bool k = true == false,
    T l = default(T),
    object m = null,
    int n = sizeof(ulong),
    params ushort[] o) where T : class, new()
                       where U : struct;

(Disappointingly had to loose a few things to get it to compile. Can't has a this parameter, because then the class must be static and static classes can't have protected members. Neither can we have the default values based on typeof(), is or as since they aren't compile time constants)

[1] http://msdn.microsoft.com/en-us/library/x53a06bb.aspx

(2) You'd need to include all primitive types - these are keywords in C# too. Also you can add default parameter value (in C# 4) e.g. object a = null. - Tomas Petricek
(1) You might as well have a parameter for every built-in C# type alias. EDIT: Make that new static extern for maximum coverage, I think, actually. Does new count as a separate keyword in the type signature and constraint? - mquander
You could add static, I think =) - Jens
you could more types instead of the int's in the parameters... - Yves M.
(2) oh yeah, I hadn't thought of generic constraints at all. Nice one. But why not make it an extension method (can put a this in there on the first argument then). - jalf
@mquander, you can't specify both virtual and override on the same method -- and if you added either, you would have to remove the static as well as the extern, so it's a wash. You could add new though. - Joe White
(2) @Tomas: and use = default(T) for another argument. default hasn't been used yet. - jalf
You can also add more generic parameters and add another generic constraint for each. - Johann Blais
(1) Oh, here's another one -- if you're using C# 4, you could add in to the generic parameter. - Joe White
@Joe: Huh, you're right. I guess it's implied. - mquander
(1) @Joe - you can only use in for contravariance on delegates and interfaces. - Mark H
There's also unsafe. I'm not sure if that plays well with extern, but you could always change static extern to new virtual. - Joe White
(1) Also, with default parameters, you can put just about any expression in there. That gets you as, is, base, null, sizeof, typeof, and possibly a few others. - Joe White
(4) Hell, you could initialize a default parameter to an anonymous delegate or a lambda: (..., Action action = () => { /* go to town */ }). Technically it's still part of the method declaration, but now you can write any statement -- so return, switch, checked/unchecked, fixed, and a whole bunch else become fair game. - Joe White
@Joe: Brilliant. You could even put a lambda, couldn't you? Then you could squeeze in a return as well - jalf
would the nullable types count as a keyword itself? - Yves M.
(3) @Joe White: I think you just blew the interviewers mind. Anybody answering with that get's the job on the spot.. - Jimmy Hoffa
Oh, and don't forget to use LINQ in the default parameters. Not sure on this one though; most of LINQ isn't technically keywords, since they're not reserved (you can declare your own variable called from). But still. - Joe White
I think that interviewer needs to specify 3.5 next time! Those defaults make it a bit rediculous. - Grant Crofton
(1) You can't use as in a default. (It must be a compile-time constant) - SLaks
(1) @Joe: You can't use LINQ or lambdas in a default either. - SLaks
(2) Now, the only question is can someone think of a suitable, self commenting, method name... - Martin Harris
Default parameter value for 'pointless' must be a compile-time constant - SLaks
2
[+9] [2010-09-15 12:50:22] Grant Crofton

I quite like it, it shows knowledge of access modifiers, variable passing, types, generics and so on, it's probably going to make the interviewee think a bit, and it's unlikely somthing they'll have learned off the Internet just to pass the interview.


(15) "unlikely something they'll have learned off the Internet"...Until now. =;) - Simon P Stevens
(2) Damn you SO users and your answers! - Grant Crofton
3
[+6] [2010-09-15 12:44:52] Justin Pinkley

From a real-world approach, the question seems silly IMHO and certainly doesn't make good sense. I don't think a definite answer exists. My guess would be that the company was just probing to see how familiar the person is specifically with C# keywords and syntax.


(4) An answer must exist. There are only a limited number of C# method declarations shorter than a gzillion characters. One of those is bound to have the most number of keywords. Still a silly question, though =) - Jens
4
[+3] [2010-09-15 14:24:27] DrDro

I think the kind of discussion we can see in the top answers' comments is what the interviewer was looking for. As often, the interviewer was less interested in the final answer then in the process. Explaining why you couldn't use this keyword in your current declaration and how you can use that one shows that you understand (some of) the main concepts.


5
[+1] [2010-09-15 13:11:46] Ben

I think an interesting approach would be to write a program that from a list of keywords and rules, constructs the longest possible function declaration. This would demonstrate even more skill on the interviewee's part and if done correctly provide without doubt the best solution.


I think Eric Lippert's "Every Program There Is" series ( see blogs.msdn.com/b/ericlippert/archive/tags/grammars ) is quite relevant to this approach. - Brian
6