class TimeObject
{
DateTime time;
bool isMatinee;
}
Given: {8:00, 9:30, 11:00, 12:10, 2:00, 4:00, 5:20} -- a collection of TimeObjects
Output: (8:00AM, 9:30, 11:00, 12:10PM, 2:00), 4:00, 5:20 -- return a string, oh and AM/PM should be picked up from localization strings
Caveats: AM/PM only shown for first time, ( ) encloses those elements whose matinee bool was set to true.
Question is: I have to figure out how to output the above string.
I mentioned, I knew C#, the interviewer was adamant to know how to do this in the fewest lines of readable code, preferably using LINQ. He said, I could write it to the console, but I had to remember to localize the AM and PM.
I obviously created a bunch of temp collections, and crap, and totally botched it up. He claims that it’s only a few lines of LINQ. I tried other things, though he kept steering me towards LINQ.
Help? Any body has ideas? This has really been cringing me the whole day now.
UPDATE – I GOT THE JOB! They also asked me Edit distance {HELLO} –> {HLO}, tell the min. number of edits/updates it’ll take to get to the final string. And there are Bees, And Honey in the world, there is 1 queen bee, the only way the honey can be accessed is through the Queen. Construct a make belief computer world that can support this, and tell if the world is in violation or not — Graph, Root node is Queen Bee, Nodes are Honey and Bee, Run BiPartite test to see if world is in violation.
[edit]
I’m sure there’s a cleaner way to do this but here’s what I have. A simple grouping would make it easier if it weren’t for the time formatting restrictions. I’ll try to come up with another version.
[edit2]
Ok, so this is very likely THE kind of answer the interviewer was looking for.
Normally when I write LINQ expressions, I always try to consider the performance. Since performance wasn’t a factor here. This should be the easiest to write and follow (but with horrible performance).
The approach, consider the time, how it’s formatted, and any prefix (an open paren) or suffix (a closing paren) when printed. As long as the
isMatineegrouping is contiguous (which I had always assumed) with times sorted, this should always work.It only has a prefix if it is the first time that is a matinee. A suffix if it is the last time that is a matinee. It should be formatted with AM/PM if it is the first time in its respective group. It should be very easy to understand.
If it were only the matinee grouping involved, I’d probably do this: