Tagged Questions
An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.
2
votes
0answers
20 views
Unstructured quantum search algorithm for unknown solutions
I put together this code for IBM's 5 qubit processor as an implementation of Grover's search algorithm, but for an unknown number of solutions:
...
2
votes
0answers
14 views
Prim's MST Java implementation
Can someone review this code, just have look if it's logically correct
or
If you may point me to a more simple and minimalist program
...
5
votes
2answers
1k views
0
votes
1answer
30 views
Circular Array Rotation C++ HackerRank [on hold]
I'm a first time poster. I conceded defeat and looked at the answers given on hackkerank itself but the ones I saw used things like vectors, which are concepts I'm a noob at as my knowledge of C++ ...
0
votes
0answers
14 views
Weird neural network behavior [on hold]
Disclamer: the code works without errors, but I've done something wrong, so the network does not work as it's supposed to.
I'm trying to write a neural network that requires as an input:
1)a list of ...
2
votes
1answer
43 views
Order Statistics Tree on top of Treap
I built an Order Statistics tree on top of a Treap in order to solve this HackerRank problem. It works (passed all test cases). Any comments whatsoever would be greatly appreciated.
...
-2
votes
1answer
53 views
Sorting using heap sort algorithm? [on hold]
I tried to write a heap sort program myself for Leetcode 217. Contains Duplicate as below rather than using Python built-in sort method. Leetcode should accept heap sort method, but for some reasons I ...
1
vote
1answer
31 views
Combine arrays and preserve ordering - but prioritize one array's ordering over another
I have two arrays and I want to combine them both in a manner similar to this:
...
4
votes
3answers
131 views
Copy range up to delimiter
The problem I was trying to solve sounded pretty basic, but turned out to be a bit hairy.
I need to select some lines of arbitrary length from an input stream (file or standard input) and print them ...
1
vote
1answer
61 views
Brute-force password cracker
I am trying to write a program to find a zip file's password. I know the password consists of only lower-case letters and it's length does not exceed 6 characters. I wanted to check the passwords of ...
2
votes
1answer
62 views
Create a unique four digit code from 35 characters
I want to generate a four digit code based on the code generated before it, starting at 0000 and going to zzzz.
These are the available characters to be inside the code:
...
2
votes
1answer
43 views
Hand crafted longest common sub sequence
I know that the below solution is not the best one and I am in the way to learn that stuff till then I have applied the brute force to see how far I can go with my intuition.
...
1
vote
1answer
47 views
Pattern searching in 2d grid
This is an interview question which i am trying to solve.
You are given a 2D array of characters and a character pattern. WAP to find if pattern is present in 2D array. Pattern can be in any way (all ...
5
votes
2answers
96 views
Predict the best day to buy and sell the shares
I was trying out my hand at solving problems and came across this HackerRank problem. I have written a solution which passes for a few test cases but times out for most others (time limit - 3 seconds)....
1
vote
1answer
57 views
Print tree level by level including Empty node
Here is my code to print tree in a level by level way. It needs to print an empty node as well to keep tree structure like full binary tree for elegancy reasons.
I'm wondering if anything you think ...
4
votes
2answers
54 views
Find Intervals with Most Overlaps
Given a list of time intervals, I want to return the intervals with the most overlaps (i.e. most people free). My solution runs in \$O(n \log n)\$, but I made a bunch of "tracker" variables. How can I ...
0
votes
1answer
78 views
Choosing Python data structures to speed up algorithm implementation [closed]
I'm given a large collection (roughly 200k) of lists. Each contains a subset of the numbers 0 through 27. I want to return two of the lists where the product of their lengths is greater than the ...
0
votes
0answers
31 views
Rating calculator [closed]
I want to rank content by rating, giving them a balance of "freshness" and "total votes up"
This is what I've come up with.
...
1
vote
0answers
34 views
Wikipath stack in Java - Part I/IV - The search algorithm
I am working on the following small software stack:
The objective of the entire stack is to search for shortest paths in the Wikipedia article graph as fast as possible. As of now, I rely on two ...
8
votes
7answers
986 views
Find out the 2 numbers which gives the highest product
Input given is an int array, which may or may not contain positive, negative or zero values. Write a
program to find out the two numbers which gives the highest product.
(Both numbers should be ...
1
vote
1answer
36 views
Check whether a given sequence can be rearranged using a stack
Working on a problem, for a given input sequence to a stack, check whether it could produce (using stack push/pop operations) the output sequence.
For example if input is ...
3
votes
2answers
116 views
Betting strategy simulation
I lately got interested into betting systems, dice strategies and algorithms. I took some time to think about it and eventually ended up with an algorithm of my own.
Considering that I don't have ...
0
votes
3answers
68 views
Calculating sum after ranged-based subtractions
The puzzle consists from something like this:
You will receive an array of integer with values, then you will receive X numbers. Each number (call it x) represents ...
7
votes
2answers
67 views
Sort items in a list by grouping rules
Problem Statement
Given a list of items each having a type associated with it, group them by the below mentioned rule
The list should not contain consecutive items that violate the ...
7
votes
4answers
1k views
Buy once and sell once for maximum profit
Suppose we have stock price history, and we are allowed to buy only once and sell only once. The problem is to find the maximum profit we can make. Here is my code that I tested for basic test cases. ...
4
votes
1answer
65 views
Pair, differences and correlations
This has a couple of twists which I personally found rather awkward... All code can be downloaded in a read-to-run format from here
The problem
Consider a classic interview question: "Find given ...
0
votes
1answer
29 views
Implementation of the binary search algorithm [closed]
This is my first implementation the binary search algorithm. I have tested it and so far it is okay. But I need an experienced eye to review the code and recommend best practices.
...
8
votes
2answers
185 views
+100
Spell checker using trie
This program implements spell checking by loading a given dictionary file into a trie structure and comparing against a given text file: ...
3
votes
2answers
70 views
HTML parsing algorithm for extracting <a> tags
My intention is to create a complete HTML parser, so far I made a basic algorithm that iterates trough text and extracts everything in an "a" tag.
It works on everything I tried, but I want a review ...
-2
votes
0answers
35 views
How can I speed up the following function in R? [on hold]
I wrote the following function in R. I want to iterate it, say 50000 times. I used "sapply" in my function but it runs slowly in R.My PC is still working about 20h now and I have no idea about the run ...
2
votes
3answers
53 views
Generating variables to be named, filled in and operated on the fly in Python
I am very new to Python and trying to refactor my code which is ripe for error.
Currently, I manually name a bunch of arrays:
...
2
votes
1answer
55 views
Counting all substrings with exactly k distinct characters
Problem Statement : Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that has exactly k distinct characters. Example:
Input: abc, k = 2 Output: 2 ...
4
votes
2answers
38 views
Finding an equilibrium index in an int array
Here is a programming challenge from codility
A zero-indexed array A consisting of N integers is given. An
equilibrium index of this array is any integer P such that 0 ≤ P < N
and the sum ...
5
votes
1answer
102 views
The Palindromic Odometer Puzzler: A Programmatic Solution
That is a programmatic solution to a puzzler from Car Talk, its summary is:
"I noticed that the last 4 digits were palindromic. I drove a mile,
and the last 5 were palindromic. I drove another ...
3
votes
1answer
68 views
My Quick sort examination
I had a test on sorting using quick sort algorithm, which I have implemented. It's working fine.
But my examiner is not satisfied. They say that the solution which I provided is not a proper ...
2
votes
1answer
29 views
Dynamic Official Receipt increment algorithm
I made a function that would automatically generate official receipt for POS. it is able to accept format such as xxx, xxx-xxx, xx-xx.
But my code is really messy and horrible and it needs review. I ...
2
votes
1answer
50 views
Total number of stops for an elevator to serve certain people
I have seen this question asked around a bit, such as this Code Review question in Java:
Write an optimal program that would determine the total number of stops
a elevator has taken to serve X ...
2
votes
0answers
71 views
Solving the Mining algorithm from HackerRank
I was working on this problem for a few hours last night and finally came up with a brute-force solution. The task is to report the minimum work necessary (sum of weight × distance) to relocate gold ...
4
votes
1answer
68 views
“Similar Destinations” challenge
I am currently solving the Similar Destinations challenge on HackerRank and am in need of some assistance in the code optimization/performance department. The task is to take a list of up to 1000 ...
7
votes
1answer
93 views
Cipher text using a 2D array
I have a simple program which ciphers text using a 2D array. You specify what letters to replace with what and it does it.
I understand the code is very repetitive and that I should probably use ...
6
votes
2answers
121 views
Accepting user defined functions for custom map reduce functionality in C++
I am implementing map and reduce - style functions for processing geospatial raster datasets.
I would like the ...
10
votes
2answers
456 views
Project Euler #2: Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will
be:
\$1, 2, 3, 5, 8, 13, 21, 34, 55, 89...\$
By ...
8
votes
1answer
137 views
Heapsort implementation in C++14
Please review the following implementation of heapsort and pay attention to the following:
Have I correctly chosen the names InputIt and ...
4
votes
2answers
85 views
Merge sorting a singly-linked list in Java - follow-up
In this iteration, I incorporated all but one points made by forsvarir in the previous iteration. Now my code looks like this:
LinkedListNode.java:
...
3
votes
3answers
72 views
Java number formatting in accountant style
I have written a string number formatter. I would like to know if this can be improved and any suggestions with how it's done.
The purpose of this function is to convert a double that has been ...
3
votes
2answers
70 views
Merge sorting a singly-linked list in Java
I have this nifty merge sort implementation for sorting singly-linked lists. Its running time is \$\Theta(n \log n)\$, yet it uses only \$\Theta(\log n)\$ space (the stack). See what I have below.
...
1
vote
2answers
59 views
Selection sort algorithm examination
I had an algorithm test.
I was asked Selection sort algorithm to program.
I coded it in javascript and shown to examiner.
But he was not satisfied, he said its too complicated.
I think I have ...
5
votes
2answers
51 views
Finding longest common prefix
I have been trying to solve a modification of the Longest Common Prefix problem. It is defined below.
Defining substring
For a string P with characters P1, P2,…, Pq, let us denote by P[i, j]
...
2
votes
2answers
42 views
Implementation of insertion sort in JavaScript
I have written an insertion sort algorithm in JavaScript:
...
10
votes
2answers
192 views
Displaying all substrings of some given string
I solved exercise 8 from Core Java for Impatient, chapter 1. This exercise is to implement a displayAllCombinations method to display all substrings of the given ...