All Questions
17 questions
1
vote
1
answer
95
views
Changes hash to allocate values as directed
Let's say you have a hashmap of sub-systems that a spaceship is currently directing power to and a second hash of sub-systems where the power should now be directed to.
Create a function that returns ...
0
votes
1
answer
340
views
Modular multiplicative inverse in Ruby
I implemented an algorithm to find the modular multiplicative inverse of an integer. The code works, but it is too slow and I don't know why. I compared it with an algorithm I found in Rosetta Code, ...
2
votes
2
answers
211
views
Given an arbitrarily large file containing integers, return the largest N numbers, highest first
I've written a top_n program in Ruby, which does pretty much exactly what the title says, as part of a coding exercise. I'm trying to learn about how to sort ...
2
votes
0
answers
435
views
Unbounded knapsack solution with items of equal value
My solution correctly solves the problem, but I would like to optimize its performance. For a capacity of 7 and bars = [ 4, 1, 1, 2, 1], the output is 7.
...
3
votes
1
answer
81
views
Turning an Integer into an array of ints
My goal here is pretty simple, I want to turn a number like 12435987234 into an array of integers in reverse order. So that would look like:
...
9
votes
3
answers
166
views
Finding if sequential numbers for total exists
Question:
Given a sequence of positive integers A and an integer T, return whether there is a continuous sequence of A that sums up to exactly T
Example:
...
4
votes
1
answer
579
views
Finding number of primes till n - Sieve of Eratosthenes
I've written a ruby implementation for Sieve of Eratosthenes algorithm, however benchmarking it against the Ruby's Prime module I have still a far slower algorithm -...
1
vote
2
answers
65
views
Deduplicate data
I currently have an implementation that helps us deduplicate data but I know it's slow.
Here's an example, note that I'm using OpenStruct here when really these are Mongoid objects pulled from the ...
13
votes
4
answers
2k
views
Count cars passing in opposite directions
The problem comes from codility, whose coding problems I'm starting to enjoy as their evaluation criteria really looks at runtime.
Task description
A non-empty zero-indexed array ...
2
votes
1
answer
223
views
Ruby on Rails Method ActiveRecord Query Optimizations
So I have three methods and I'm not sure how to optimize them. I will be working on this and updating this post while I do but basically running these three methods is central to some parts of my ...
0
votes
2
answers
546
views
Fastest way to find maximum deviation
I acknowledge that this exact question was asked here. I was working on the same problem from the same website. I had the same question and consulted the above as a reference, however with respect ...
1
vote
1
answer
692
views
Speed up anagram method in Ruby
I'm working on a project to find anagrams of words. My goal is to figure out the longest anagrams out there. I took a look at my code and it works but is woefully inefficient.
I have two arrays, each ...
5
votes
2
answers
3k
views
Reverse Game HackerRank Ruby Solution
Problem Statement
Akash and Akhil are playing a game. They have N balls numbered from 0
to N−1. Akhil asks Akash to reverse the position of the balls,
i.e., to change the order from ...
4
votes
2
answers
2k
views
Performance of Codility MaxCounter challenge solution
I'm doing some of the Codility challenges using Ruby. The current challenge is "The MaxCounter," which is described as:
Calculate the values of counters after applying all alternating operations: ...
4
votes
1
answer
490
views
Optimize a zscore algorithm
I wrote a zscore algorithm in Ruby that runs fine, but is incredibly slow when I have 8000+ entries to score. Can anyone help me figure out a way to improve my code, please?
...