Algorithms | Learn for Master - Part 4
• ### LeetCode – Logger Rate Limiter

Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.

Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.

It is possible that several messages arrive roughly at the same time.

Example:

Logger logger = new Logger();

// logging string “foo” at timestamp 1
logger.shouldPrintMessage(1, “foo”); returns true;

// logging string “bar”

• ### PHP sort Arrays

In the previous tutorial, we have used examples to learn PHP arrays.  In this post, we talk about how to sort php Arrays based on different needs.

Here are the functions we can use to sort a PHP array:

• rsort() Sorts an indexed array in descending order
• sort() Sorts an indexed array in ascending order
• arsort() Sorts an associative array in descending order, according to the value
• asort() Sorts an associative array in ascending order, according to the value
• krsort() Sorts an associative array in descending order,
• ### Java Sort using Comparable or Comparator

In Java, it is straightforward to sort objects of the predefined class such as Integer, Double, Float and String. This is because these classes have been implemented the Comparable interface.  Comparable defines a natural ordering, as when you’re defining it, you specify which one is  considered “less than” or “greater than” the another.

How can you sort custom Objects? For example, suppose you have a list of objects of Student class, which has name, age, and score attributes. You may want to sort the students by their scores. Another example is to sort a list strings by their length.

• ### Scala for loop

In this post, we list the common ways of for loop in Scala programming.

Scala for loop with ranges

The simplest syntax of for scala loop with ranges is as follows:

i to j can also be replaced with other generators such as  i until j.  See the following example:

The output is:

```value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 5```

Scala for loop with multiple ranges

We can use multiple ranges separated by semicolon (;) within scala for loop.

• ### Leetcode – Permutations ( Java)

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

Analysis

I will use an example to illustrate how to generate all the permutation of an array.

Given a list [1, 2, 3, 4],  all the permutations  consists of the four sets:
the permutations starts with 1:  {1} + {permutations of array [2, 3, 4]}
the permutations starts with 2,
the permutations starts with 3,
the permutations starts with 4,

Suppose we have a function called search to generate permutations for the subarray nums[start .. end].

• ### Leetcode – Search for a Range (Java)

Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm’s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

Analysis:

This problem is equivalent to find the lower bound and upper bound of the target value in the sorted array.

For instance, given a sorted array [1,

• ### Resources to prepare a technical Interview

Here are some good resources to prepare a technical interview about programming, software engineering, algorithms, data structures, machine learning, and data science.

Popular websites to prepare a technical interview

GeeksforGeeks.org: A famous website for detailed explanation for essential algorithms and data structures.

CareerCup.com  The website built by the author of the one of the most famous book CC150. You can find many interview questions of big companies.

Glassdoor.com : A platform to rate companies. For each company, you can find some interview questions posted by its users.

• ### Remove Element from an Array (Java)

LeetCode – Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

Analysis

This problem is similar to another leetcode problem: Remove Duplicates from Sorted Array

• ### Remove Duplicates from Sorted Array (Java)

Leetcode: Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

Analysis

We defined two indexes: pre and cur.

• ### Leetcode – Remove Nth Node From End of List (Java)

Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.

For example,

```   Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.
```

Note:
Given n will always be valid.
Try to do this in one pass.

Analysis:

The key to solve this problem is to use two pointers: pre and cur. The pre pointer moves N steps in the first place,