Sunday, May 8, 2016

HackerRank: Linked List - Find Merge Point of two linked list

May 8, 2016

Problem statement:
https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists

Cpp solution:

https://gist.github.com/jianminchen/ce99505020ee58f2fd23ecd78bb2f19f

Value simple, easy question, solve the problem, learn from other submissions. From the simple ones, build up skills, feel more comfortable to solve moderate, difficult one later.

Submissions:
https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists/leaderboard

Julia's favorite code:
1. define a few functions, very well written:
length, position, common - use recursive function, very short function - 3 lines of code each function
https://gist.github.com/jianminchen/2ab387360a0ff2ef41c10c67219fd1c1
source:
https://goo.gl/J5WK7y

2. unbelievable short - only use for loop to do things:
https://gist.github.com/jianminchen/9d14ff515146af193c4bee660b0a6fb8

Question and answer:
1. What do you learn through the problem solving? Code submission study?

Julia likes to document the problems she found when she reads first 100 solutions, in order to improve the speed, accuracy, and avoid bugs - avoid repetition of same logic:

1. Best code she found so far:
https://gist.github.com/jianminchen/2ab387360a0ff2ef41c10c67219fd1c1

2. One is to use brute force, go through each node in the first list, compare to each node in the second list; The algorithm is not optimal.

3. Shortest time to write the code, in less than 20 lines:
https://gist.github.com/jianminchen/9d14ff515146af193c4bee660b0a6fb8

3. Repeat same code twice - if/else statement - avoid two cases, simplify to one case.

4. Same code as Julia does, but better code:
https://goo.gl/NXTurm

No comments:

Post a Comment