## Dynamically create hash from array of arrays

I want to dynamically create a Hash without overwriting keys from an array of arrays. Each array has a string that contains the nested key that should be created. However, I am running into the issue where I am overwriting keys and thus only the last...
more »

## algorithm: partition 2 arrays into "only in A", "intersection of A & B", & "only in B"

I have a situation where I have two arrays and I need to partition them so that I end up with 3 arrays: elements that are only in A elements that are only in B elements that are in both A and B Example: A = [1, 4, 3, 2] B = [2, 6, 5, 3] 3part(A,...
more »

## Removing an input from a recursive method

Good morning! I received a problem statement to write a method that returns all possible combinations of a String input passed, e.g. if ABC is passed then it returns [A, AB, BC, ABC, AC, B, C] if ABCD is passed then it returns [A, AB, BC, CD, ABC,...
more »

## Efficient algorithm to get a new element in a set

Hello fellow programmers, I will start with a contrived example. Lets say you are in charge of conducting an athletic meet for a school. School consists of number of classes. Each class will send some athletes for the meet. Each athlete will have ...
more »

What should the algorithm for resolving symlinks on Linux look like? Something like: Split path to steps /usr/bin/hello -> ['usr', 'bin', 'hello'] First resolve /usr -> /something1 Add next step and resolve /something1/bin -> /something2 Add next ...
more »

## Given an array find the number of sub arrays with m odd numbers?

Sample Input: 1 2 3 4 5 (array elements) m = 1 (Odd numbers) Sample output: 8. The subarrays are [[1], [1,2], [2,3], [2,3,4], [3], [3,4], [4,5], [5] ] Here is my implementation.In the worst case, it would take O(n+n^2).Are there any ways to optimiz...
more »

## Get list of combinations for K groups of N members and L groups of M members

In Python; what is the best way to get a list of combinations for k groups of n members and l groups of m members given a list of possible members g? Example, given a list of elements: g = ["A", "B", "C", "D", "E", "F", "G"] What I want is to hav...
more »

## Getting all possible down- and right edge paths in a n x n grid

Problem 15: Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 20×20 grid? So my attempt at Problem 15 is kinda bruteforcy because I try to g...
more »

## Sequential citation numbering in R: separate numbers by hyphen, if sequential - add comma if not

I want to generate sequential citation numbers for a figure in R. The numbers should be separated by a hyphen, if they are sequential. Otherwise the numbers are separated by a comma. For example, numbers 1, 2, 3, 5, 6, 8, 9, 10, 11 and 13 should come...
more »

## Remove zero entries from an array in C

I have an array of values x = {0,0,1,2,3,0,0,7,8} and I want to remove the zero entries using C. Attempt: I am attempting to loop through each value in the array and check if the entry is not equal to zero. If this condition is true, then I am att...
more »

## Least Recently Used (LRU) Cache

I know that I can use various container classes in STL but it's an overkill and expensive for this purpose. We have over 1M+ users online and per user we need to maintain 8 unrelated 32-bit data items. The goal is to find if an item exists in the...
more »

## Algorithm - Maximum left sub-array with lesser elements

I am working on a program where I need to get the index of element in an array of integers such that all elements to the right of the index are greater than all the elements from 0 to that index position. For example: Case : 1 - Given input - { 5, ...
more »

## A* star algorithm open and closed lists

I have looked at the implementation of the problem on the internet and i have a question: whenever you add a node you need to search it in the open list(why it is not enough to search it in the closed list?)? And why do you search it to see if you ca...
more »

## 2D Greedy Ascent Search Algorithm Clarification

I am doing some remedial work on algorithms as I am taking a graduate course on them in the Fall and was a physics undergrad. Watching this video and at the 38:00 mark he goes over the greedy ascent algorithm for a 2D array. I am confused as he defin...
more »

## Algorithm - longest wiggle subsequence

Algorithm: A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequenc...
more »

## fastest starts with search algorithm

I need to implement a search algorithm which only searches from the start of the string rather than anywhere within the string. I am new to algorithms but from what I can see it seems as though they go through the string and find any occurrence. I ...
more »

## Searching for indexes of multiple subtrings in multiple files

I've got two dataframes which are as follows: df1 : contains one variable ['search_term'] and 100000 rows These are words/phrases I want to search for in my files df2: contains parsed file contents in a column called file_text There are 20000 ...
more »

## Algorithm to get which values make sum of a given number from array

I don't know to search or google it so I ask it here. I have an array of integers with fixed size and exactly with this logic. sample [1,2,4,8,16,32] Now I am given a number for example 26. And I shall find the numbers whose sum will make this nu...
more »

## Function to generate flight trajectory (list of 3D points, lat, lon, alt)

I am looking to generate some 3D trajectory data for an aircraft simulation. The idea is that the aircraft takes off at some location x and continues to ascend at some average ascent velocity a_v and angle a_theta until it reaches a maximum altitude ...
more »

## Understanding Bitwise OR '|' in Ruby example

I was working on implementing a recursive powerset algorithm in Ruby and I came across this Stack Overflow post. def powerset(set) return [set] if set.empty? p = set.pop subset = powerset(set) subset | subset.map { |x| x | [p] } end pow...
more »

## Code submission on Spoj is giving runtime error (NZEC)

I am struggling with Runtime Error (NZEC) on Spoj for problem, http://www.spoj.com/problems/NHAY/ I tried so many cases from my side and everytime it is giving correct output in eclipse but could not able to find out the reason of Runtime error whi...
more »

## Array find value using index logic interview questions

Below is given array for infinite length which has natural numbers as it can be infinite length: int[] myArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 1, 1, 2, 1, 3 ......}; // at place of 10 it'll take 1, 0, at place of 11 it'll take 1, 1, at ...
more »

## Finding all pairs with difference of 's' in an array

I have an array in length n with integers in the range [0,n^5]. I want to find all pairs in the array for which the difference between them is a given integer variable of value s (for example for integers a,b in the array, we will either have a-b=s o...
more »

## how to reverse random.seed in Python

I want a way to give me the seed given a random number it could have generated. for example, if I have the following code import random seed = 3 lower_bound = 1 upper_bound = 10 random.seed(seed) random.randint(lower_bound, upper_bound) given t...
more »

## Find time complexity of a code that runs three loops

I have this piece of code and I would like to find its time complexity. I am preparing for interviews and I think this one is a bit tough. int foo (int n) { int sum = 0; int k, i, j; int t = 2; for (i=n/2; i>0; i/=2) { ...
more »

## Radius Range based search using a Ball Tree?

I've currently created a ball tree based on the design seen here: http://www.astroml.org/book_figures/chapter2/fig_balltree_example.html My implementation looks like this: class BallTree: def __init__(self, data, level): self.data = No...
more »

## How to remove an object from other keys in a dictionary [Objective-C]?

I have an NSMutableDictionary as follows: { 0 = (1,5,6); 1 = (0,2,6,7); 2 = (1,7,8); 5 = (0,6,10,11); 6 = (0,1,5,7,11,12)}; in the format of {NSNumber:NSMutableArray} I want to remove every 0 that is there in every key or the keys for the valu...
more »

## Efficient algorithm to get the combinations of all items in object

Given an array or object with n keys, I need to find all combinations with length x. Given X is variable. binomial_coefficient(n,x). Currently I'm using this: function combine(items) { var result = []; var f = function(prefix, items) { ...
more »

## How can I sort DOM elements triggering the least amount of reflow?

I've got the following example. <div class="parent"> <div data-id="5"></div> <div data-id="2"></div> <div data-id="3"></div> <div data-id="1"></div> <div data-id="4"></div> ...
more »

## Ruby algorithm to determine valid HTML structure

I have to as input data an array with hashes, each hash is description of an html tag (open and end position in the text and type of the tag). I need to generate another array where tags are put in order. For example: input = [ {start_p: 0...
more »

## push and pop of integers to stack, what outcome is not possible

I am trying to take an online course of algorithms and i cant seem to get my head around how this works. (this is not homework, just a question from a free online course) would anyone explain to me how the answers are found? the are given at the end...
more »

## Algorithm for converting large hex numbers into decimal form (base 10 form)

I have an array of bytes and length of that array. The goal is to output the string containing that number represented as base-10 number. My array is little endian. It means that the first (arr[0]) byte is the least significant byte. This is an exam...
more »

## Count number of tails since the last head

Consider a sequence of coin tosses: 1, 0, 0, 1, 0, 1 where tail = 0 and head = 1. The desired output is the sequence: 0, 1, 2, 0, 1, 0 Each element of the output sequence counts the number of tails since the last head. I have tried a naive method:...
more »

## How does array access affect the performance?

int steps = 256 * 1024 * 1024; int[] a = new int[2]; // Loop 1 for (int i=0; i<steps; i++) { a[0]++; a[0]++; } // Loop 2 for (int i=0; i<steps; i++) { a[0]++; a[1]++; } Can someone explain why the second loop is 20x times slower than the fi...
more »