## How to efficiently build a tree from a flat structure?

I have a bunch of objects in a flat structure. These objects have an ID and a ParentID property so they can be arranged in trees. They are in no particular order. Each ParentID property does not necessarily matches with an ID in the structure. Therefore their could be several trees emerging from these objects. … Read more

## How to find the lowest common ancestor of two nodes in any binary tree?

The Binary Tree here is may not necessarily be a Binary Search Tree. The structure could be taken as – struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something of this sort – Consider this binary tree : The inorder … Read more

## Listing all permutations of a string/integer

A common task in programming interviews (not from my experience of interviews though) is to take a string or an integer and list every possible permutation. Is there an example of how this is done and the logic behind solving such a problem? I’ve seen a few code snippets but they weren’t well commented/explained and … Read more

## Select N random elements from a List in C#

I need a quick algorithm to select 5 random elements from a generic list. For example, I’d like to get 5 random elements from a List<string>. 32 Answers 32 Using linq: YourList.OrderBy(x => rnd.Next()).Take(5)

## Performing Breadth First Search recursively

Let’s say you wanted to implement a breadth-first search of a binary tree recursively. How would you go about it? Is it possible using only the call-stack as auxiliary storage? 22 Answers 22

## Rolling or sliding window iterator?

I need a rolling window (aka sliding window) iterable over a sequence/iterator/generator. Default Python iteration can be considered a special case, where the window length is 1. I’m currently using the following code. Does anyone have a more Pythonic, less verbose, or more efficient method for doing this? def rolling_window(seq, window_size): it = iter(seq) win … Read more

## Algorithm to find Largest prime factor of a number

What is the best approach to calculating the largest prime factor of a number? I’m thinking the most efficient would be the following: Find lowest prime number that divides cleanly Check if result of division is prime If not, find next lowest Go to 2. I’m basing this assumption on it being easier to calculate … Read more

## What is the fastest substring search algorithm?

OK, so I don’t sound like an idiot I’m going to state the problem/requirements more explicitly: Needle (pattern) and haystack (text to search) are both C-style null-terminated strings. No length information is provided; if needed, it must be computed. Function should return a pointer to the first match, or NULL if no match is found. … Read more

## Rounding up to the nearest multiple of a number

OK – I’m almost embarrassed posting this here (and I will delete if anyone votes to close) as it seems like a basic question. Is this the correct way to round up to a multiple of a number in C++? I know there are other questions related to this but I am specficially interested to … Read more

## How does finding a cycle start node in a cycle linked list work?

I understand that Tortoise and Hare’s meeting concludes the existence of a loop, but how does moving tortoise to the beginning of linked list while keeping the hare at the meeting place, followed by moving both one step at a time make them meet at the starting point of the cycle? 23 Answers 23