Interview Algorithms

From David's Wiki
Jump to navigation Jump to search

Insights from Leetcode problems.

Finding a cycle in a linked-list

Use two runners.
Runner 1 goes two steps per iteration.
Runner 2 goes one step per iteration.
If there is a cycle, runner 2 will lap runner 1 within 2 cycles.

Finding duplicates in an array

If you have an array of ints where each number appears times and one number appears times where , then you count the number of times each bit appears and take it mod .
The remaining bits will remain times.