## Why Time complexity of permutation function is O(n!)

Consider following code. public class Permutations { static int count=0; static void permutations(String str, String prefix){ if(str.length()==0){ System.out.println(prefix); } else{ for(int i=0;i<str.length();i++){ count++; String rem = str.substring(0,i) + str.substring(i+1); permutations(rem, prefix+str.charAt(i)); } } } public static void main(String[] args) { permutations(“abc”, “”); System.out.println(count); } } here the logic, that i think is followed is- it … Read more

## Shortest path (fewest nodes) for unweighted graph

I’m trying build a method which returns the shortest path from one node to another in an unweighted graph. I considered the use of Dijkstra’s but this seems a bit overkill since I only want one pair. Instead I have implemented a breadth-first search, but the trouble is that my returning list contains some of … Read more

## Check if any item in a list matches any item in another list

A coleague asked me to write a one-liner to replace the following method: public static bool IsResourceAvailableToUser(IEnumerable<string> resourceRoles, IEnumerable<string> userRoles) { foreach (var userRole in userRoles) foreach (var resourceRole in resourceRoles) if (resourceRole == userRole) return true; return false; } Resharper and I came up with this: public static bool IsResourceAvailableToUser(IEnumerable<string> resourceRoles, IEnumerable<string> userRoles) { … Read more

## A fast array shift implementation in C#?

I need to shift to the right and to the left an array by N places. The items that pop out on the side where i shift to must get back into on the other side. Shift right by 13: [0,1,2,3,4,5,6,7,8,9] -> [7,8,9,0,1,2,3,4,5,6] Shift left by 15: [0,1,2,3,4,5,6,7,8,9] -> [5,6,7,8,9,0,1,2,3,4] This operation will happen millions … Read more

## Artificial Intelligence Methods to Detect Cheating in Games [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 1 year ago. Improve this question My day job is for an online browser based game, one that is small, with … Read more

## Data structure for handling intervals

I have got a series of time intervals (t_start,t_end), that cannot overlap, i.e.: t_end(i) > t_start(i+1). I want to do the following operations: 1) Add new (Union of) intervals [ {(1,4),(8,10)} U (3,7) = {(1,7),(8,10)} ] 2) Take intervals out [ (1,7) – (3,5) = {(1,3),(5,7)} 3) Checking whether a point or a interval overlaps … Read more

## Why is Depth-First Search said to suffer from infinite loops?

I have read about DFS and BFS many times but I have this doubt lingering my mind since long. In a lot of articles it is mentioned that DFS can get stuck in infinite loops. As far as I know, this limitation can easily be removed by keeping track of the visited nodes. In fact, … Read more

## In Big-O notation for tree structures: Why do some sources refer to O(logN) and some to O(h)?

In researching complexity for any algorithm that traverses a binary search tree, I see two different ways to express the same thing: Version #1: The traversal algorithm at worst case compares once per height of the tree; therefore complexity is O(h). Version #2: The traversal algorithm at worst case compares once per height of the … Read more

## Find the largest k numbers in k arrays stored across k machines

This is an interview question. I have K machines each of which is connected to 1 central machine. Each of the K machines have an array of 4 byte numbers in file. You can use any data structure to load those numbers into memory on those machines and they fit. Numbers are not unique across … Read more

## Print out all permutations of an Array [duplicate]

This question already has answers here: Permutation of array (13 answers) Closed 6 years ago. I am working on a program, and I have a function that swaps the positions in an Array of length that is input by a user. However, I am trying to figure out how to print out this function call … Read more