Showing posts with label kindergarten adventures. Show all posts
Showing posts with label kindergarten adventures. Show all posts

Sunday, March 19, 2017

code review: Hackerrank kindergarten adventures

March 19, 2017

Problem statement

Code review is posted here.

C# code - still work on more test cases on API testing, Modify and Query.

Introduction


Julia starts to learn binary index tree and segment tree through hackerrank university codesprint #2 in November 2016, she tried a few times but failed each time. She knew that she is better to work on the algorithm "kintergarden adventure" and learn from the algorithm.

She also did post the question on segment tree algorithm on code review to ask help on Dec. 10, 2016, and then the question "kintergarden adventure" was closed. Through the incident, Julia knew that she was afraid to learn by herself.

Julia is very comfortable at data analysis, so in order to figure out the algorithm, Julia chose to have some test case study, put together some data, and then taught herself what to look for through those tables.

Test case study


For example, there are 20000 students in the circle, and the first student only need to 0 minute to finish drawing, so that if the teacher starts from any students from ID = 1 to 20000, the first student can complete the drawing. SegmentTree class Modify API has to take the task to mark those 20000 nodes as value 1, it is not scalable for 3 seconds time limit, so that we can use up to logN intervals to cover the range of [1, 20000].

To make it simple, we assume that the range's width is 1024 instead of 20000, and see how many steps we need to mark in tree[]. Not up to 1024, but in the level of logN = log1024 = 10.


Here is the SegmentTree class Modify API: To understand the test case, in order to modify 1024 nodes as 1, we only do it in less and equal to 10 times, here is the two images to explain the detail.

The two variables of left and right are iterated from beginning to end 10 times, each iteration two variables's values are recorded in the table.

Let us get our hands dirty on this test case, RunTestcaseModify3() line 12, tree.Modify(0,1024,1). We look into the function call.

First row of table, left = 20001, right = 21024, since Modify API is called and function arguments: start = 0, count = 1024, value = 1,
read Modify API code line 5 and line 6, left is calculated as 20001 and right is calculated as 21024.


and more detail is here:

Action items


Review code review Hackerrank Modular Range Queries
Read the tutorial of segment tree.
Learn binary index tree from topcoder

Inspiration


This is the first time Julia started to use Microsoft Excel to do some test case analysis to help her understand the segment tree algorithm design.

All it takes is for her to have some patience. She read those two tables built by Microsoft Excel, and then she asks herself what is missing, what problems she can tell from those data. After a few times search, she comes out ideas to move forward her study.






Wednesday, November 16, 2016

HackerRank - university codesprint - kindergarten adventures (after the contest)

Nov. 16, 2016

It is time to learn Segment Tree quickly. Julia worked on Segment Tree a few times in 2016, but when she worked on the algorithm in the contest, she did not come out the idea using segment tree to solve the problem. Segment tree is also called binary index tree.

The best learning experience is a failure. She will remember forever how segment tree is applied to a real story - kindergarten adventures.

Problem statement

Previous blogs about segment tree:

1. Oct. 18, 2016
Segment Tree Tutorial

2. Sept. 25, 2016
Range Minimum Query blog

3. March 4, 2016
HackerRank: Bear and Steady Gene (I)

Study C# submissions:

1. C# code # 1
2. C# code #2
3. C# code #3
4. C# code #4
5. C# code #5

Study JavaScript submission

Study Java submission

There are over 60 solution to score 30 (maximum score 30), go over one by one, and learn a few tips from those solutions. Write down what you like most - 3 things in Java code.

1. Very structured code

2. Java code to study

No. 1
No. 2
No. 3

C solution

Julia's ideas to improve the performance:

1. Try to write some code first, or do some research to categorize the problem, narrow down the algorithm problem - segment tree. (Quickly go over competition books, search ideas!)

2. Either improving the research of categorizing the problem to classical problem, or be practical, using brute force solution to get a few points. (Just do it!)

3. Give up the effort on advanced algorithm, only focus on one of medium algorithms. If Julia work on this medium algorithm in the contest, put 10 hours work on it, then she could score full score. (Aim low target first!)

4. Try to get in Bronze medal first. Work on medium algorithm (maximum score 30) instead of advanced algorithm (maximum score 80). (Play safe! From medium to advanced)

5. Try to work on an algorithm in 2 hours range - Do not spend more than 2 hours on a problem. First work on medium algorithm each one 2 hours first. Increase the chance to score more points.