Find Longest Common Prefix in an Array of Strings

How to find longest common prefix in an array of strings.

Given an array of strings, write a method to find the longest common prefix string. If no common prefix is found, return an empty string ” “.

For example:

Example 1:

Input: [“cat”,”cable”,”camera”]
Output: “ca”

The longest common prefix is ca.

Example 2:

Input: [“rat”,”dog”,”elephant”]
Output: “”

No common prefix is found.

Detect Loop in Linked List

How to detect loop in linked list.

Given a linked list, determine if it has a cycle or loop in it.

For example:

In the below example, the last node (whose value is 9) points to node whose value is 5. The last node of a linked list does not points to a null. It has a cycle.

Detect a loop in a linked list

Detect loop in a linked list