My first Codility test experience

My first Codility test experience

Last week, I received an email from a recruiter who asked me to do Codility assessment. I had to answer three questions within 245 minutes. Although I’ve heard about Codility, this was my first Codility test experience. To be honest, it was really difficult to me.

These questions were not easy to understand, so it took me some time to understand each question. Then I tried to answer the questions by Java or Python programming language. Even though I eventually found the solutions and summited the answers, my average score was not good enough. When I finished this assessment, I was very disappointed about myself. I even thought whether I should keep a programmer job.😂😂😂

It seems that more and more companies like to use Codility assessments to evaluate developers, but I don’t think it is a best way to test a programmer’s coding/programmer abilities.

Task 1: Find the maximum amplitude of paths consisting nodes in a binary tree

codility_q1

I answered this task in Java programming language:

class Solution {    
     class Result{
        public int min;
        public int max;
    }

    public int solution(Tree T) {
        if(T == null){
            return 0;
        }
        Result res = helper(T);
        return res.max - res.min;
    }

    public Result helper(Tree T) {
        Result res;
        if (T == null) {
            return null;
        }

        Result left = helper(T.l);
        Result right = helper(T.r);

        if (left == null && right != null) {
            res = update(right, T.x);
        } else if (left != null && right == null) {
            res = update(left, T.x);
        } else if (left != null && right != null) {
            res = update(left, right, T.x);
        } else {
            res = update(T.x);
        }

        return res;
    }

    private Result update(Result c, int i) {
        Result res = new Result();
        if (i < c.min) {
            res.min = i;
            res.max = c.max;
        } else if (i > c.max) {
            res.max = i;
            res.min = c.min;
        } else {
            res.max = c.max;
            res.min = c.min;
        }
        return res;
    }

    private Result update(int i) {
        Result res = new Result();
        res.min = i;
        res.max = i;
        return res;
    }

    private Result update(Result left, Result right, int i) {
        Result res = new Result();
        int l = 0;
        int r = 0;
        l = Math.abs(i - left.min) > Math.abs(i - left.max) ?
                Math.abs(i - left.min) :
                Math.abs(i - left.max);
        r = Math.abs(i - right.min) > Math.abs(i - right.max) ?
                Math.abs(i - right.min) :
                Math.abs(i - right.max);
        l = l > Math.abs(left.min - left.max) ? l : Math.abs(left.min - left.max);
        r = r > Math.abs(right.min - right.max) ? r : Math.abs(right.min - right.max);
        if (l > r) {
            if (i < left.min) {
                res.min = i;
                res.max = left.max;
            } else if (i > left.max) {
                res.max = i;
                res.min = left.min;
            } else {
                res.max = left.max;
                res.min = left.min;
            }
        } else {
            if (i < right.min) {
                res.min = i;
                res.max = right.max;
            } else if (i > right.max) {
                res.max = i;
                res.min = right.min;
            } else {
                res.max = right.max;
                res.min = right.min;
            }
        }
        return res;
    }
}
(Visited 1,072 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail
Comments are closed.