Testing candidate's mettle S02E03
-
@cartman82 said in Testing candidate's mettle S02E03:
This version is actually a bit more complicated, if you're gonna do it right.
Can you do it right, though? Consider the following dataset:
Buy Coke ========== Yes please Yes please Not gonna Yes please
Is this one column, or is it two columns? Even the most sensible definition of a column start (in each row there's a non-space character at that index preceded by whitespace) trips up on that. Capital letters won't save you either.
The first question you'd get from me would be to clarify what can and what can not be assumed about the dataset.
-
@Maciejasjmj said in Testing candidate's mettle S02E03:
Is this one column, or is it two columns? Even the most sensible definition of a column start (in each row there's a non-space character at that index preceded by whitespace) trips up on that. Capital letters won't save you either.
In the reference solution I presume 2 spaces minimum between columns.
https://bevel-gambler.hyperdev.space/
You're right, there's no 100% correct solution.
But that's often the case in reality as well. The few times when candidates pressed me, I told them "Imagine this is some legacy crap owned by some ignoramus client, made years ago by his nephew. He has no idea how data might change or when it comes from. What you see is all you know." Makes things more interesting.
-
@cartman82 said in Testing candidate's mettle S02E03:
But that's often the case in reality as well. The few times when candidates pressed me, I told them "Imagine this is some legacy crap owned by some ignoramus client, made years ago by his nephew. He has no idea how data might change or when it comes from. What you see is all you know." Makes things more interesting.
Our previous product manager tended to refuse further explanation with the statement "You know what I want. Just go build it."
-
@cartman82 said in Testing candidate's mettle S02E03:
It took me like 30-40 min to code a reference solution.
15 mins for me, complete with a jinja template generating a html table.
So does your company do anything interesting? :P
-
@Adynathos we produce a bitchin blog series on programming interviews.
-
@cartman82 It's practically a new category of stories for TDWTF front page all by itself :D
-
@cartman82 You could have a TV show where candidates try to write the program, the jury ridicule their code, and the loser falls into a pit of manure.
-
I'm not afraid :
using System; using System.Collections.Generic; using System.Linq; using System.IO; namespace Cartman { class Program { static void Main(string[] args) { var file = File.OpenText("D:\\data.txt"); // OK I assume the data are already there. Beat me. //header Lines file.ReadLine(); file.ReadLine(); List<Record> lr = new List<Record>(); do { var line = file.ReadLine(); var r = Record.Parse(line); if (r.Keep) lr.Add(r); } while (!file.EndOfStream); var l = (from r in lr orderby -r.UnitPriceNumeric select r.FullLine); foreach ( var s in l ) { Console.WriteLine(s); } Console.ReadLine(); //pause to contemplate the result. } } class Record { public static readonly Tuple<int, int> ProductNameColumn = new Tuple<int, int>(0, 24); public static readonly Tuple<int, int> UnitColumn = new Tuple<int, int>(24, 24); public static readonly Tuple<int, int> AmountColumn = new Tuple<int, int>(48, -1); public string FullLine; public string ProductName; public string UnitPrice; public decimal UnitPriceNumeric { get { return Convert.ToDecimal(UnitPrice, System.Globalization.CultureInfo.InvariantCulture); } } public string Amount; public decimal AmountInGram { get { var tokens = Amount.Split(' '); if (tokens.Length != 2) { throw new ArgumentException("Amount"); } decimal count = 0; count = Convert.ToDecimal(tokens[0].Trim(), System.Globalization.CultureInfo.InvariantCulture); var unit = tokens[1].ToLowerInvariant().Trim(); decimal multiplier = 1; switch(unit) { case "g": multiplier = 1; break; case "kg": multiplier = 1000; break; default: throw new ArgumentException(" unknown unit -" + unit + "-"); } return count * multiplier; } } public bool Keep { get { return AmountInGram >= 750; } } private static void ParseField(string line,Tuple<int, int> FiledDesc, out string Field) { if(FiledDesc.Item2 != -1 ) { Field = line.Substring(FiledDesc.Item1, FiledDesc.Item2).Trim(); } else { Field = line.Substring(FiledDesc.Item1).Trim(); } } public static Record Parse(string fileLine) { Record r = new Record(); r.FullLine = fileLine; ParseField(fileLine, ProductNameColumn, out r.ProductName); ParseField(fileLine,AmountColumn,out r.Amount); ParseField(fileLine, UnitColumn, out r.UnitPrice); return r; } } }
Done in half an hour, without mucking with MSDN, only Intellisense.
The code has few merits beside fulfilling the question.
-
#!/usr/bin/env python3 import jinja2 class Entry: def __init__(self, text_line): tokens = [s.strip() for s in text_line.split(' ') if len(s) > 0] self.name = tokens[0] self.price = float(tokens[1]) self.amount = self.parse_amount(tokens[2]) def parse_amount(self, am_str): elems = am_str.split(' ') val = float(elems[0]) unit = elems[-1] if unit == 'kg': val *= 1000 return val with open('data', 'r') as f_in: lines_in = f_in.readlines() #headers are line 0 #=== is line 1 entries = [Entry(line) for line in lines_in[2:]] entries = [e for e in entries if e.amount >= 750] entries.sort(key = lambda e: -e.price) template = jinja2.Template(""" <table> {% for e in entries %} <tr> <td>{{ e.name }}</td> <td>{{ e.price }} $</td> <td>{{ e.amount }} g</td> {% endfor %} </table> """) with open('out.html', 'w') as f_out: f_out.write(template.render(entries=entries))
-
@asdf said in Testing candidate's mettle S02E03:
Yeah, but after years of PHP, I learned to ignore shitty names for standard library functions. I almost don't notice it anymore. Of course this function should be named
output_file
orpass_trough_file
or something like that."Trough" as in "the thing swine eat from"? Yeah, sounds like PHP
-
-
@Johan_B said in Testing candidate's mettle S02E03:
var r = Record.Parse(line);
Objection! The logic on how to parse the file does not belong to the Record class.
(also, you hard-coded the field ranges and your design seems really overcomplicated)
-
@cartman82, where did the original file go? I'm bored ATM and wanted to write my own solution.
-
- Where would you put the parsing logic? I put it in the record.Parse() like Integer.Parse() or DateTime.Parse()
- Time was at a premium, so hardcoding fields was the way to go.
- Now that I think of it, I should have written the C# equivalent of Adynathos' solution
I'd like @cartman82 to come back and tell if both of us would have gotten the ball rolling.
-
@Johan_B said in Testing candidate's mettle S02E03:
Where would you put the parsing logic?
Outside, in its own function.
I put it in the record.Parse() like Integer.Parse() or DateTime.Parse()
Integer or date representations are more or less universal. Records on the other hand can be represented in many different ways (tab-separated, comma-separated, XML, JSON). If you had to read them from different providers you'd probably need a specific parser for each one.
-
@anonymous234 said in Testing candidate's mettle S02E03:
date representations are more or less universal
Date representations? I'm gonna go with “less universal” here.
-
Full-on complicator gloves solution. Assumes as little as possible about the data. Ran a bit overtime with it, though - about an hour and a half to implement.
-
@cartman82 said in Testing candidate's mettle S02E03:
Let's analyze his dev environment apps.
Sublime. WAMP. Filezilla. Photoshop. uTorrent.
Old school windows hacker.
expectations--
.In defense, I've used WAMP for very quick tests with PHP. Like, if a customer using our API had a question about it, I'd just fire it up and do a quick proof-of-concept. I don't use it often enough to have it on my friggen taskbar, however.
The uTorrent makes me hope he never uses this computer for real-life projects. Depending on how he uses it, his computer could be riddled with malware and viruses.
@cartman82 said in Testing candidate's mettle S02E03:
Candidate has 10 years of professional contractor experience. Decided to do it in PHP.
I continue to be flabbergasted by the number of people who claim this kind of experience (sometimes backed up with references and other somewhat credible evidence) who fumble through exercises like these, and of course use Stack Overflow as their immediate knee-jerk reaction instead of, you know, just thinking about the task at hand.
@cartman82 said in Testing candidate's mettle S02E03:
I hear toilet flushing.
He literally shat his pants when he saw the test?Indicates lack of planning. You always clean the tubes before a big interview.
@cartman82 said in Testing candidate's mettle S02E03:
Are you fucking shitting me? He opened some PHP 101 course examples, and is going through them.
Kind of like a pilot who decides 500 feet before landing to check out some manuals and coursework on how to do it.
@cartman82 said in Testing candidate's mettle S02E03:
Me: Soooo.... how's it going?
Candidate: I never worked with files earlier, so I got a little stuck
Me: Do you think you'll figure it out in the next 10-15 mins?
Candidate: No I don't think so.
Me: OK.... well... thank you for this interview, and.... ummm.....
Candidate: OK, bye. (click)
He couldn't shut down teamviewer fast enough. Glad I got that screenshot, for posterity.Gotta give him credit for presumably understanding that he totally flunked. Once again, using a pilot analogy, this is like saying, "I've never worked with the altimeter, so I got a little stuck."
On the plus side, if he's never worked with files, I guess that does at least indicate he has never implemented a flat file relational database like a lot of PHP hackers often do, so there's that.
-
@The_Quiet_One said in Testing candidate's mettle S02E03:
The
uTorrentweb browser makes me hope he never uses this computer for real-life projects. Depending on how he uses it, his computer could be riddled with malware and viruses.
-
@cartman82 but he also has SourceTree. So that's something, right?
-
-
@asdf said in Testing candidate's mettle S02E03:
@cartman82, where did the original file go? I'm bored ATM and wanted to write my own solution.
I changed my mind about posting it in text form, I don't want it getting into google. I just reuploaded it as zip, hopefully google doesn't do anything to clever with it.
-
@Johan_B said in Testing candidate's mettle S02E03:
I'd like @cartman82 to come back and tell if both of us would have gotten the ball rolling.
You mean if you'd have passed the test? Well, duh. I'm sure anyone here would.
-
@The_Quiet_One said in Testing candidate's mettle S02E03:
In defense, I've used WAMP for very quick tests with PHP. Like, if a customer using our API had a question about it, I'd just fire it up and do a quick proof-of-concept. I don't use it often enough to have it on my friggen taskbar, however.
WAMP + FileZilla tell me he has zero devops / linux chops.
He probably just hacks php on his Windows PC, then copies the files directly to server. Probably no version control at all.@The_Quiet_One said in Testing candidate's mettle S02E03:
On the plus side, if he's never worked with files, I guess that does at least indicate he has never implemented a flat file relational database like a lot of PHP hackers often do, so there's that.
When I heard that excuse, I knew he was hopeless.
Even after an hour spinning in circles, he still thought this task had anything to do with files.
-
@Arantor said in Testing candidate's mettle S02E03:
@cartman82 but he also has SourceTree. So that's something, right?
I didn't recognize that icon.
Kudos to him for at least using some VCS.
-
@cartman82 said in Testing candidate's mettle S02E03:
@Arantor said in Testing candidate's mettle S02E03:
@cartman82 but he also has SourceTree. So that's something, right?
I didn't recognize that icon.
Kudos to him for at least using some VCS.
Yeah, it's the newer icon from the post-Win10 compatibility when they Win10-ified it a few versions ago.
-
@cartman82 said in Testing candidate's mettle S02E03:
I don't want it getting into google.
Check if there is a question about your task on stackoverflow :P
-
@cartman82 said in Testing candidate's mettle S02E03:
@asdf said in Testing candidate's mettle S02E03:
@cartman82, where did the original file go? I'm bored ATM and wanted to write my own solution.
I changed my mind about posting it in text form, I don't want it getting into google. I just reuploaded it as zip, hopefully google doesn't do anything to clever with it.
@cartman82 said in Testing candidate's mettle S02E03:
@asdf said in Testing candidate's mettle S02E03:
@cartman82, where did the original file go? I'm bored ATM and wanted to write my own solution.
I changed my mind about posting it in text form, I don't want it getting into google. I just reuploaded it as zip, hopefully google doesn't do anything to clever with it.
My solution has a link to the file uploaded as .txt on WTDWTF, if someone wants to have their code grab it off the 'net (not sure if it's a requirement).
-
@Maciejasjmj
For the WTDWTF difficulty level, it should be mandatory to retrieve and postback the results via AJAX
-
@izzion said in Testing candidate's mettle S02E03:
AJAX
That... might be challenging without either Javascript or XML
-
@Maciejasjmj but you do have languages that can do HTTP, so you can do all the legwork of an HTTP request with the appropriate X-Requested-With header for that authentic AJAX feel!
-
@Adynathos said in Testing candidate's mettle S02E03:
@cartman82 said in Testing candidate's mettle S02E03:
I don't want it getting into google.
Check if there is a question about your task on stackoverflow :P
really we should stage questions and answers on SO for cartman's task, but make sure the answers have some slight issue that prevents them from being totally correct. that way he could catch the cargo culters, and maybe someone who will catch it and try to fix it.
-
He has 1 second of PHP experience 315569260 times.
-
@ben_lubar Maybe more than a second but probably still in the minutes category.
-
@fwd said in Testing candidate's mettle S02E03:
but make sure the answers have some slight issue that prevents them from being totally correct
Have a code sample with wrong spelling (using some unicode wizardry maybe?) or with library functions that look sensible but don't exist in reality.
-
@fwd said in Testing candidate's mettle S02E03:
@Adynathos said in Testing candidate's mettle S02E03:
@cartman82 said in Testing candidate's mettle S02E03:
I don't want it getting into google.
Check if there is a question about your task on stackoverflow :P
really we should stage questions and answers on SO for cartman's task, but make sure the answers have some slight issue that prevents them from being totally correct. that way he could catch the cargo culters, and maybe someone who will catch it and try to fix it.
Better yet find a way to have the answers be completely off the wall wrong... like have someone ask how to parse a CSV and do some sorting followed by a reply with a 1000-line function that does a bunch of pointless stuff and in the end just prints a recipe for gourmet tiramisu. Would love to hear the muttering of "what the fuuuuck?" after copy pasting that whole thing top to bottom without reading it and then trying it out.
-
@Adynathos said in Testing candidate's mettle S02E03:
@cartman82 You could have a TV show where candidates try to write the program, the jury ridicule their code, and the loser falls into a pit of manure.
The Apprantice?
-
@dse I'm sure there's a joke to be had in the misspelling of The Apprentice to include 'rant'. I just can't see it right now though.
-
@The_Quiet_One said in Testing candidate's mettle S02E03:
reply with a 1000-line function that does a bunch of pointless stuff and in the end just
prints a recipe for gourmet tiramisusends us their paypal login cookiesYou know, some fair compensation.
-
@Maciejasjmj said in Testing candidate's mettle S02E03:
@izzion said in Testing candidate's mettle S02E03:
AJAX
That... might be challenging without either Javascript or XML
Surely someone must have made a Javascript interpreter in PHP.
-
@anonymous234 said in Testing candidate's mettle S02E03:
Surely someone must have made a Javascript interpreter in PHP.
Failing that, you can fall back to invoking the v8 JS engine from PHP: https://secure.php.net/manual/en/v8js.examples.php
-
@DCoder said in Testing candidate's mettle S02E03:
@anonymous234 said in Testing candidate's mettle S02E03:
Surely someone must have made a Javascript interpreter in PHP.
Failing that, you can fall back to invoking the v8 JS engine from PHP: https://secure.php.net/manual/en/v8js.examples.php
Shit, someone actually did that?
$php_hatred++;
-
@Arantor said in Testing candidate's mettle S02E03:
@dse I'm sure there's a joke to be had in the misspelling of The Apprentice to include 'rant'. I just can't see it right now though.
@Arantor said in Testing candidate's mettle S02E03:
$php_hatred++;
Arantor The PHP Apprantist
-
@dse I should build a language, it'd be tremendous, the best language. I'd make the best language. The best. And it'd be tremendous. It'd be so good, and it'd only cost 4 man years to build. I know some people, some good people, we'd build it in less than 8 man years. Ask Sean Hannity, he'll tell you we know some good people and how we could build it in less than 10 man years.
-
@fwd said in Testing candidate's mettle S02E03:
@Adynathos said in Testing candidate's mettle S02E03:
@cartman82 said in Testing candidate's mettle S02E03:
I don't want it getting into google.
Check if there is a question about your task on stackoverflow :P
really we should stage questions and answers on SO for cartman's task, but make sure the answers have some slight issue that prevents them from being totally correct. that way he could catch the cargo culters, and maybe someone who will catch it and try to fix it.
@anonymous234 said in Testing candidate's mettle S02E03:
@Maciejasjmj said in Testing candidate's mettle S02E03:
@izzion said in Testing candidate's mettle S02E03:
AJAX
That... might be challenging without either Javascript or XML
Surely someone must have made a Javascript interpreter in PHP.
I sense an opportunity!
Evaluate this JavaScript, using PHP.
Okay....
JavaScript in PHP
I got this now!
:
Sorry for my failure to properly Nagesh. Just pretend they got an editor.
-
@Arantor said in Testing candidate's mettle S02E03:
@dse I should build a language, it'd be tremendous, the best language. I'd make the best language. The best. And it'd be tremendous. It'd be so good, and it'd only cost 4 man years to build. I know some people, some good people, we'd build it in less than 8 man years. Ask Sean Hannity, he'll tell you we know some good people and how we could build it in less than 10 man years.
We're going to build a walled garden around your language's context. It'll be a great walled garden, the best. And we'll use your language's compiler and CPU resources to enforce it.
We need this walled garden. When your language is sending it's interprocess communications, it's not sending it's best. They're sending communications with lots of problems. They're sending malformed packages. They're bringing buffer overflows. They're bringing command injections. They're bringing malware. They're rapists. And some, I assume, are validated packages.
-
@Arantor said in Testing candidate's mettle S02E03:
It'd be so good, and it'd only cost 4 man years to build.
Provided it is interpreted, you can write a language in a lot less than 4 man-years. Compiled implementations — especially if optimising and going to native code — take a lot more work to write. The language standard library will take a heck of a lot more work unless you're singularly frugal (or piggy-back off some existing ecosystem like the .NET CLR or the JVM).
-
@dkf said in Testing candidate's mettle S02E03:
@Arantor said in Testing candidate's mettle S02E03:
It'd be so good, and it'd only cost 4 man years to build.
Provided it is interpreted, you can write a language in a lot less than 4 man-years. Compiled implementations — especially if optimising and going to native code — take a lot more work to write. The language standard library will take a heck of a lot more work unless you're singularly frugal (or piggy-back off some existing ecosystem like the .NET CLR or the JVM).
The whoosh badge will be tremendous. The best whoosh badge ever.
..the entire post you've quoted from was a Trump-esque rant. :P
-
@Arantor said in Testing candidate's mettle S02E03:
..the entire post you've quoted from was a Trump-esque rant.
Oh? Is that what he talks like? I don't listen to that sort of thing. (My preferred news sources are tuned to exclude as much politics as possible.)
-
@dkf said in Testing candidate's mettle S02E03:
My preferred news sources are tuned to exclude as much politics as possible
you and me both.
-shudder- Politics scares me.