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.
-1
votes
0answers
16 views
how to refactor multiple for loops? [on hold]
Note: Please do not use any String functions. No sorting allowed. No additional arrays or data structures allowed.
An array is said to be hollow if it contains 3 or more zeros in the middle that are ...
4
votes
2answers
105 views
3
votes
2answers
218 views
Adjusting dates in an array based on a start date
I have a data structure like this which holds start/end dates:
...
2
votes
0answers
51 views
Hackerrank: Sherlock and anagram
Problem statement
Given a string \$S\$, find the number of "unordered anagrammatic pairs" of substrings.
Input Format
First line contains \$T\$, the number of testcases. Each testcase ...
7
votes
3answers
69 views
Leetcode 56: Merge Intervals
Problem statement
Given a collection of intervals, merge all overlapping intervals.
For example:
Given \$[1,3],[2,6],[8,10],[15,18]\$,
return \$[1,6],[8,10],[15,18]\$.
My ...
1
vote
0answers
24 views
second degree connection ranking
Working on below second degree connection ranking problem. Post the problem and my code, my current solution is, find all first degree connection, and then for each first degree connection, I try to ...
1
vote
1answer
30 views
Hackerrank Gemstones Solution
I'm looking for any possible improvements in terms of memory usage and speed, but really any advice is welcome.
Problem statement:
John has discovered various rocks. Each rock is composed of ...
3
votes
2answers
71 views
4 sum challenge (part 2)
This is a continued discussion from (4 sum challenge) by return count only.
Problem
Given four lists A, B, C, D of integer values, compute how many tuples
(i, j, k, l) there are such that A[...
1
vote
1answer
58 views
Longest absolute path in file system
Problem
Suppose we abstract our file system by a string in the following
manners:
The string dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext represents:
...
2
votes
1answer
63 views
String similarity algorithm (c++) [on hold]
Parameters:
first - the first string.
second - the second string.
similarCharacters - ...
2
votes
3answers
169 views
Different path for grid move (part 3)
This is a continued discussion from (Different path for grid move (part 2)) to optimize for space complexity (using only cur list, other than a ...
4
votes
0answers
84 views
4 sum challenge
Problem
Given four lists A, B, C, D of integer values, compute how many tuples
(i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
To make problem a bit easier, all A, B, C,...
4
votes
3answers
97 views
Two sum with two lists
Suppose we have two sorted lists, and we want to find one element from the first, and the other element from the 2nd list, where the sum of the two elements equal to a given target. If there are ...
1
vote
0answers
30 views
Atomic singly linked list
Here's some code I've written for an MMO server I'm working on. I'd love some opinions and eventually unspotted bugs.
...
6
votes
4answers
239 views
Balanced Parentheses checker in Python
I'd appreciate feedback on the code itself (style, performance optimization (both space and time)), and also on the algorithm (because I think this is typically implemented using a stack).
The ...
1
vote
0answers
27 views
Beginning of any maximum ascending slice of an array A of maximum size
I want to write a function public int solution(int[] A) that, given a zero indexed array A consisting of ...
3
votes
1answer
71 views
Eulerian Path algorithm
I'm doing a project to find the Eulerian path. Cane someone find an example where the algorithm is wrong? The function EulerianPath recursively prints the Eulerian ...
2
votes
3answers
82 views
Different path for grid move (part 2)
This is a continued discussion from (Different path for grid move) to optimize for space complexity, and since it is new code and I make a new post.
Given a m * n grids, and one is allowed to move ...
3
votes
0answers
68 views
Implement SGD of the softmax regression with only numpy
I am implementing the stochastic gradient descent algorithm. I think there is plenty of room for improvement.
...
6
votes
0answers
49 views
Vergesort — A runs-adaptive layer to enhance sorting algorithms
More than a full-fledged sorting algorithm, vergesort is a Runs-adaptive algorithm that falls back most of the time to another sorting algorithm to sort a sequence of elements, but is able to identify ...
5
votes
1answer
166 views
Hackerank - value of friendship
Problem statement
You're researching friendships between groups \$n\$ of new college students where each student is distinctly numbered from \$1\$ to \$n\$. At the beginning of the semester, no ...
4
votes
1answer
84 views
Different path for grid move
Given a m * n grids, and one is allowed to move up or right, find the different number of paths between two grid points.
My major idea is, if move r steps right, <...
3
votes
1answer
80 views
KMP algorithm searching from the right
The generic version of extension method is aimed to work like String.LastIndexOf which can search t from the last position of <...
2
votes
1answer
119 views
Count the number of times a particular number appears in a sorted array of integers
I had an interview question awhile back that went more or less as follows:
You have a sorted array of integers representing the ages of every person living on earth. There's a single entry in the ...
0
votes
0answers
43 views
+50
Implementation of a KNN in OCaml
I wrote the following implementation of the k-nearest neighbor algorithm (for a binary classification task). I am not familiar with OCaml's built in functions, I have the feeling that some of them ...
0
votes
1answer
106 views
Round Robin implementation in java
I tried to implement round robin in java please check and help me to
improve it Thanks:
...
135
votes
4answers
20k views
Dijkstra path finding in C# is 15x slower than C++ version
I'm implementing Dijkstra's algorithm with a priority queue for a game I'm developing in Unity with C#. I was a bit disappointed with the performance, so I decided to port the code to C++ and see if ...
4
votes
1answer
47 views
Single Linked List Implementation in Ruby
The #traverse_until method is a helper function that traverses the linked list and returns the node and index of the node once a condition (in a block) is met or until the end of the list. I would ...
2
votes
2answers
62 views
Java class for sorting arrays based upon QuickSort algorithm
Hint: This question is a follow-up to my Code Review question from 09.01.2017: Class sort with BubbleSort algorithm
After been showed Mergesort and QuickSort as pseudo-code this week in lecture ...
4
votes
4answers
297 views
Find element that has been removed from a shuffled array
For a function to determine which element has been removed from a shuffled array, I came up with:
...
-6
votes
0answers
29 views
Can we reduce the Complexity using segment tree in this code? [closed]
Can we reduce the complexity using segment tree here or any other means? if yes, How?
it's a code for the finding longest chain like the example below:
if
...
1
vote
0answers
42 views
A* Algorithm in F#
Inspired by this post I looked up A* on wikipedia and went on with my own implementation as seen below where I try to mimic the pseudocode on Wikipedia but in a recursive manner. I would like any ...
1
vote
1answer
45 views
3
votes
0answers
40 views
Counting squares in a grid that is subdivided by line segments
Problem:
Given a picture of square with a bunch of horizontal and vertical lines in it (lines are not necessarily spanning the full square length, in other words think of a fine grid with many ...
1
vote
1answer
26 views
2 Queens on N x N board
I am trying to write an algorithm to solve a variation of the N queens problem. There are only 2 queens on an N x N board. I simply need to find the number of solutions, don't need to know what the ...
1
vote
2answers
321 views
Dual state button algorithm in C
I wrote code for determining the push button state whether it is long pressed or not. This function is called by timer interrupt routine every 1ms.
But it seems really dumb. How can I make it shorter ...
1
vote
2answers
33 views
Count Characters Function PHP
As part of studying algorithms, I needed to make a function to count occurrences of characters in a string. My solution is below. I'd like some feedback about how I did - could the implementation be ...
3
votes
1answer
68 views
Given two words, transform the first one into the second [closed]
As the title says, the problem I had to do sounded something like this:
We are given two words, S1 and S2. We must transform S1 into S2, using the following operations:
insert: insert a character ...
5
votes
1answer
87 views
Find equal pairs
Problem
Given an array A of unique integers, find the index of values that
satisfy A + B =C + D, where A,B,C & D are integers values in the
array. Find all combinations of quadruples.
I ...
3
votes
2answers
78 views
Write a Java class “Sort” which implements the BubbleSort algorithm
For a further education I had to do the following task:
Implement the BubbleSort algorithm within the Java class
algorithm.Sort.
Submit your solution to ...
1
vote
2answers
50 views
Delta encoding a list of numbers
Given a list of numbers, e.g.:
25626 25757 24367 24267 16 100 2 7277
Output a delta encoding for the sequence. In a delta encoding, the first
element is ...
3
votes
3answers
67 views
Continuous sequence against target number
Post problem and my code written in Python 2.7. Any advice for code bugs, performance in terms of algorithm time complexity and code style are appreciated. I did PEB8 self-check in PyCharm.
BTW, a ...
2
votes
1answer
48 views
Ashton and String Hackerrank
Online coding challenge Hacker Rank.
I tried to solve it using the naive appraoch first but its failing on some of the inputs and rest its getting timed out. How to get started in problem like these ...
3
votes
1answer
84 views
Find kth largest element in the union of two sorted array
Problem statement:
Find \$kth\$ largest element in the union of two sorted array.
My introduction of the algorithm
I spent a few hours to review two algorithms, Leetcode 4:Median of Two Sorted Arrays ...
2
votes
0answers
75 views
Hackerearth Simple Function - Follow-up
Problem statement
Let us first describe a SimpleFunction:
...
3
votes
0answers
35 views
Tarjan's strongly connected component finding algorithm
Here is my code for Tarjan's strongly connected component algorithm. Please point out any bugs, performance/space (algorithm time/space complexity) optimization or code style issues.
...
1
vote
1answer
90 views
Build String Hackerrank
Online challenge on Hacker Rank.
Please follow the description from the above link.
...
7
votes
3answers
307 views
Homework / toy program - character matching
Given a string A = "Hello world" and another string B = "wor", I have to print ("TRUE" or "FALSE") whether the characters in <...
-6
votes
1answer
110 views
Word frequency counter - symbol table - array implementation
Symbol table
It is key-value pair abstraction
Insert a value with specified key
Given a key, search for the corresponding value
Here is the design for symbol table implementation for frequency ...
5
votes
1answer
75 views
Custom Big Integer class for Project Euler in C#
I'm trying to teach myself some programming and, working through the Project Euler problems, I've come across some instances where I've needed numbers that are larger than will fit into an int or long ...