Interview: task to solve at home

Interview: task to solve at home
0.0 0

#1

Hello All FCC Campers!

I was on the interview last Monday to get a Junior PHP Developer job. There were common HR questions like: ‘do you like to work in a group?’ , ‘did you have a situation where you didn’t achieve result you wanted?’ , ‘with what kind of people you like working with?’ , ‘what motivates you?’, etc.

Then there was a test covering CSS, HTML, HTML5, jQuery, JavaSrcipt, SQL. Unfortunately I don’t remember questions because I’ve got only 20 minutes to solve all of them.

Then I’ve go technical interview. There were questions like: ‘what is the difference between abstract class and interface?’ , ‘can interface have private methods?’, ‘what characterizes REST?’, 'what does class property “polymorphism” mean? ', ‘what dependency injection is?’ and so on but I can’t recall more.

This all took about 1,5 h. At the end I was given a task to do at home. And as a matter of fact I write all of this because I need some help :slight_smile: .

The task is:

Please implement a tool to convert tabular data between any of the supported data types. Supported types are: json, xml, table html, ul/li html, ascii table, csv.
The tool should be easy to extend by a further data types.

So all great but I have a problem with last sentence. I’m not sure how to divide classes so it would be easy to extend. And I’m thinking if I should use JavaScript to convert it? Can anyone give me advice on this?

I assumed that in form you choose data type and paste a code of table in chosen data type. Then you choose data type you want the table to convert into. The output is a code of table in chosen data type.
So the input and output are strings.

Thank you very much for your help!

I also encourage you to try and solve this task by yourself. I do it in PHP but you can choose whatever object programming language.

If you have any questions about interview then go ahead and ask :slight_smile:


#2

Hi. I have absolutely no knowledge of php, so I’ll just tell you how I would do it in javsscript, assuming that it can be done in php as well:

I’d have an object that maintains a map/dictionary/associative array/etc. of input converters and another containing output converters. The keys would be the names of the formats each can convert from or to. I would decide on an internal format to use, in js it could be an array of objects or something like that, depending on the definition of ‘tabular data’. Input converters convert tabular data into that internal format while output converters convert the internal format into the output format:

function TabularConversion() {
  this.inputConverters = {};
  this.outputConverters = {};
  // ...
}

TabularConversion.prototype.addInputConverter = function(converter) {/*...*/};
TabularConversion.prototype.addOutputConverter = function(converter) {/*...*/};

TabularConversion.prototype.convert = function(inputFormat, outputFormat, input) {
  assert(this.inputConverters.hasOwnProperty(inputFormat));
  assert(this.outputConverters.hasOwnProperty(outputFormat));
  var inputConverter = this.inputConverters[inputFormat];
  var outputConverter = this.outputConverters[outputFormat];
  return outputConverter(inputConverter(input));
};

// ...

function convertFromJson(json) {
  return JSON.parse(json);
}

function convertToXml(data) {
  return MyXmlLib.toXml(data);
}

var conversion = new TabularConversion();
conversion.addInputConverter(convertFromJson);
conversion.addOutputConverter(convertToXml);
conversion.convert('json', 'xml', '{}');

I’m using functions as interfaces, but you might as well use proper classes/interfaces if they are available. Extending my converter to understand other formats is done by registering two functions. For convenience, the constructor could take multiple converters or an external/static function could return a new instance with the requested formats registered.


#3

Thank you so much!! :slight_smile:
I’m not good at JavaScript so I didn’t fully understand your code but it surely gave me understanding of your idea to solve that task. And what saved me was your idea of internal data (the simplest solustions sometimes are the hardest to see :wink: ). So thank you so much again!