Notes on codes, projects and everything

More Interviewing Questions

Just survived a job interview, so I should probably celebrate this despite the outcome. Well, considering I was off the job market for a couple of years, I probably has all the reason to be nervous. Anyway, like most geeky serious job interview, there are a test given by the company to the attendees.

I am not really a theory-citing kind of person, so I probably suck at all the questions that require explaining concepts. They are kind enough to spare me from the Python fundamentals part of the test (knowing my main language is PHP). So for this post I am going to post just the part that is on algorithm.

There were two questions in this part of the test, and they are rather interesting. Most people would have done these especially when starting to pick up a language. I have definitely done it in Clojure, and probably also in PHP, Javascript and others somehow or other.

While they said it would be better if I could do it in Python, but I would probably take too much time figuring out the syntax and stuff. For the purpose of proving my understand to the problem, I did them in PHP anyway.

The first question called for the first n sum of Fibonacci series, with n = 100. I used to do it with recurring function, but without an interpreter nearby I came out with another solution. Probably just as bad as the recurring function counterpart in terms of complexity, but it does the job (I am not really an efficient coder, optimization always comes later). I screwed up in retrieving the previous two fib(n) numbers, and is fixed in the following solution. For the record, they start the sequence as follows: 1, 2, 3, 5, … (instead of 0, 1, 1, 2, 3, …).


<?php
echo array_reduce(
    array_reduce(
        range(1, 100),
        function($current, $next) {
            if($next == 1) {
                array_push($current, 1);
            } else if($next == 2) {
                array_push($current, 2);
            } else {
                array_push($current, $current[$next - 3] + $current[$next - 2]);
            }
 
            return $current;
        },
        array()),
    function($current, $next) {
        return $current + $next;
    },
    0);

The second one was about calculating the sum for a given row or column from a two-dimensional array (matrix?). I am sure like the above solution, there must be a more efficient version around.


echo call_user_func(
    function(Array $data, $type = 'row', $index = 10) {
        return array_reduce(
            $type == 'row' ?
                array_reduce(
                    $data,
                    function($result, $next_row) use($index) {
                        array_push($result, $next_row[$index]);
 
                        return $result;
                    },
                    array())
                : $data[$index],
            function($current, $next) {
                return $current + $next;
            },
            0);
    },
    array(
        array(0, 1, 2, 3),
        array(1, 2, 3, 4),
        array(2, 3, 4, 5)),
    'row',
    1);

There are of course a lot of missing stuff here and there (like validation of the array, and index etc.), and this is just a proof-of-concept solution. Overall, I am happy I sort of got it right except the minor mistake I made in the first question, where I screwed up the index calculation. Anyway, this is still a really interesting test (reminds me of fizz-buzz somehow).

leave your comment

name is required

email is required

have a blog?

This blog uses scripts to assist and automate comment moderation, and the author of this blog post does not hold responsibility in the content of posted comments. Please note that activities such as flaming, ungrounded accusations as well as spamming will not be entertained.

Click to change color scheme