Build hierarchy from Stack-like log

process log with stack java

When the input data is a flat list, you may notice sometimes they contains the data consists of “start” and “end” pairs. Every “start” has a corresponding “stop” message, which is in a stack-like fashion. When analyzing this kind of data, it will be helpful to build hierarchy from stack-like …

Continue reading

Clean directories with recursion – Code

clean folder

Clean directories with recursion is to uses Java File class and methods to remove files in a file system. What is recursion? Recursion is a technique that a function or an algorithm calls itself. It is powerful tool to solve math, and data structures problems. It solves sub and repetitive …

Continue reading

Sort squares with optimization – Code

sort square

Sort squares is to sort the square of elements in a sorted array. Since it uses sorting algorithms, the time complexity is O(n^2). Now you are asked to optimize it so that The complexity should be O(n). To optimize, We can use the “merge two sorted arrays” approach. It is …

Continue reading

Selection sort (2 solutions) – Code

selection sort

Selection Sort is one of three well-known simple sorting algorithms (ie bubble sort, insertion sort and selection sort). It is intuitive. Starting from the first element of the array, find the smallest one in each iteration, and put it at the front. As other two simple sorting algorithms, we can …

Continue reading

Merge two sorted arrays (2 solutions) – Code

merge two arrays

To merge two sorted arrays in one sorted array, the efficient way is to loop through and compare each element in two arrays, put the smaller one in the result array. Eventually you will get one sorted array. If you are asked to improve this method, you can do it …

Continue reading