Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [algorithm]

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.

0
votes
0answers
45 views

Check if list contains 3 elements with predefined sum

I have 2 solutions about puzzle I mentioned in title: solution 1( O(n^3) ): ...
1
vote
0answers
26 views

Finding All Cycles (Verticies may be visited more than once, edges may not)

Given the undirected graph above, find cycles 1->2->3->1 4->5->3->4 1->2->3->5->4->3->1 I tried using this pseudocode (from ...
2
votes
3answers
51 views

Javascript run length encoding algorithm

I have implemented a custom run length encoding algorithm that converts an m x n array into an p x (n+1) array. The extra column is to record counts. Probably easier to show through examples: <...
0
votes
1answer
20 views

Mapping properties between two Objects of different structure

Something I've found myself having to do reasonably often is map properties from an Object (usually a heavily nested MongoDB document) to a different 'shape' to then pass it to a function call or API ...
0
votes
1answer
43 views

Rabin-Karp String matching

I am learning algorithms and have implemented the Rabin-Karp string search algorithm. I didn't implement a rolling HashCode, but it's judged correct by this ...
1
vote
0answers
25 views

Javascript time series data combination

I've written code that performs a merge of two streams of sorted time series data. The first stream is taken to be the primary and any successive streams are merged into it, using the nearest ...
1
vote
0answers
39 views

Java breadth-first search for finding the shortest path on a 2D grid with obstacles

Please review my Breadth-first search algorithm implementation in Java for finding the shortest path on a 2D grid map with obstacles. The findPath() method ...
1
vote
0answers
33 views

Correctness of iterative DFS for cycle detection

I'm attempting the Graph Algorithms section in CLRS right now. Here's an iterative DFS using a stack called nodes. ...
1
vote
3answers
70 views

Determine a list contains two elements with a given sum

On interview I was asked to write method with following contract: boolean checkList(List<Long> list, long sum){...} for example it has to return true for ...
-3
votes
0answers
22 views

switch case inside for loop cant work c [closed]

I'm Trying to use switch case inside for loop but apparently it wont work ...
0
votes
0answers
21 views

Inserting nodes to the end of a linked list by function [closed]

I am trying to insert nodes to the end of a linked list by function. This is the insertNode function: ...
2
votes
1answer
55 views

Pythonic quick sort

I'm new to the world of python, and wrote this quick sort implementation which was taught to us in an algorithms class. I would like to learn to write code less like C++ and more pythonic. Could you ...
12
votes
2answers
2k views

Tower of Hanoi Recursive Implementation using Python with OOP

I have just finished a small program for Tower of Hanoi using recursion. I did it to practice OOP which I recently learnt. It took me about 2 hours to research about the recursive algorithm and write ...
-3
votes
1answer
37 views

Given an array of positive integers, find shortest subarray whose sum exceeds a threshold [closed]

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. Example: Input: ...
0
votes
0answers
24 views

Python: Shared Nearest Neighbor (SNN) Clustering Algorithm

I wrote my own Shared Nearest Neighbor(SNN) clustering algorithm, according to the original paper. Essentially, I get the nearest neighbors for each data point, precompute the distance matrix with ...
1
vote
1answer
88 views

Quicksort Without Recursion

Am teaching myself about sorting techniques and trying my hand at a quicksort implementation. I found a lot of examples based on recursion but didn't want to go that route in a language that doesn't ...
1
vote
2answers
114 views

Luhn Algorithm “modulus 10” checksum

I wanted to challenge myself in a new language that I am learning (C#) by doing a simple exercise: check if a "modulus 10" Luhn number is valid or not. In this case the common application if verifying ...
1
vote
1answer
95 views

Remove all duplicated elements in a single list

I am learning algorithms. And my current algorithm is create a single list, and try to remove all duplicated elements in a sorted list. Ex: given Input: 1 -> 1 -> 1 -> 2 -> 2 -> 3 -> 5 -> 5 -> 7 ...
2
votes
2answers
50 views

Given an array of numbers, return array of products of all other numbers

I was recently asked the following interview question over the phone: Given an array of integers, produce an array whose values are the product of every other integer excluding the current index. ...
0
votes
0answers
12 views

Iterative DFS for computing vulnerabilities in a connected Undirected Graph

So I'm been attempting CLRS's Graph Algorithms section lately. There are a few questions on Articulation Points and Bridges in a connected Undirected Graph, even ...
7
votes
4answers
376 views

Binary Heap (list-based)

This is my first attempt at implementing a heap data structure and I've decided to cover the methods Peek, Pop, ...
4
votes
2answers
94 views

Find minimum XOR values in a given array

I am implementing an algorithm to find the pair of integers in the given array which have minimum XOR value. Ex: Input: 0 2 5 7 Output: ...
1
vote
1answer
28 views

Efficiently determining maximum allowed euclidean distance between lists of colors

I was recently tasked with determining which hex RGB colors in list color_list are nearest to each hex RGB color in list target_colors, using euclidean distance as the measuring stick, and only ...
0
votes
0answers
28 views

Iterative DFS for computing timestamps for all nodes in a Directed Graph

I'm attempting the Graph Algorithms section in CLRS right now. Here's an iterative DFS using a stack called nodes. ...
2
votes
0answers
60 views

Longest Palindromic Substring challenge

My code is a mix-up between dynamic programming with the HashMap and brute force with the way I reach out the results and substring. I don't know how to optimize my ...
1
vote
0answers
15 views

SpaceSaving frequent item counter in Python

In the code below, I implemented the SpaceSaving frequency estimation algorithm described in this paper. Given a parameter eps, the algorithm finds all elements of ...
8
votes
2answers
101 views

Where is the most desolate place in Colorado?

The Story While flying a plane over the forests of Wisconsin a couple years ago, I thought to myself "If I lost an engine, I would be awfully long way from an emergency landing airport. I wonder ...
1
vote
3answers
74 views

Counting paths between 3 cities in C++

The task is to make a program that prints out how many possible paths a driver can use to visit three cities so that he visits each city exactly n times. The program must be executed within 1 minute ...
3
votes
2answers
100 views

Calculating a Luhn Check Digit

I was able to find all kinds of examples that demonstrate how to validate a Luhn Checksum but very few that actually generated the check digit. Here is my attempt based on the validation examples ...
2
votes
3answers
111 views

Fastest way to determine if a number is perfect square

I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer). I was asked this in an interview recently. Here is the very simple and ...
2
votes
1answer
50 views

Print out all possible letter combinations a given phone number can represent

I am working on a problem in which given a 7 digit telephone number, we need to print out all possible combinations of letters that each number could represent. I came up with below code and I was ...
0
votes
0answers
15 views

Implement an algorithm to find the Kth to last element of a singly linked list

To start with, I know that this question has been answered in Java and C++, but I was wondering if it were right to answer it in JS. And in the event that it is, then whether this solution can be ...
1
vote
2answers
13 views

Implement an algorithm to find the Kth to last element of a singly linked string

I wrote this algorithm to find the Kth element of a singly linked list and I would like your feedback because I want to know if there is a better version. In my eyes, this is the most optimized way ...
1
vote
1answer
103 views

Efficiently sample a single row of an array at random

I have the following C++ code snippet that randomly samples a single row in an array called pop with num_specs columns and ...
2
votes
2answers
119 views

BalancedPassword Codility task

Ok, now for some time I've been trying to conquer this challenge without success performance wise. And I'd like some hints about approaching this problem, or the general algorithms I should learn ...
2
votes
2answers
59 views

Simple implementation of LRU caching in PHP

This is my implementation of LRU caching in PHP. It works fine when I tested as. What are the things that I miss or need attention? ...
1
vote
1answer
50 views

Space Invader game written In VBA, 3rd iteration [closed]

Here is a link to the workbook, classes, modules and forms: https://github.com/Evanml2030/SpaceInvader/tree/master Positive: I have decoupled the view from the control / presenter. I have ...
1
vote
1answer
47 views

Shuffling sections of an ordered list based on tuple values

I have a list as follows and I want to shuffle the elements that have the same frequency while keeping the elements with different frequencies in the correct positions. The code below works; however, ...
1
vote
1answer
77 views

Smallest job first algorithm implementation

I have implemented smallest job first algorithm using javascript. It was part of a coding challenge. I would like to ask for evaluation of my script. I would like to get suggestions to improve my code ...
3
votes
1answer
42 views

Hungarian algorithm to perform least-cost assignments

The Hungarian algorithm solves the assignment problem, and I'm looking for any suggestions regarding improvement on my implementation (also coding style). It is based on the wikipedia entry and ...
0
votes
3answers
36 views

Subarray Sum Equals K

I've written a solution to the following leetcode problem: Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example ...
3
votes
0answers
44 views

Word search algorithm

I have a Word Search algorithm, the main function (wsearch) takes a grid of letters as an input and a 'word' to search for. The output is the number of occurence of ...
3
votes
1answer
62 views

Count contiguous sub-array whose sum is a perfect square

I have to count the number of contiguous sub-arrays whose sum is a perfect square. For instance, if we have an array: number = [1,4,2,3,5] Now the contiguous ...
3
votes
1answer
64 views

Non-recursive BFS Maze Solving with Python

I wrote this program to help me understand how BFS and Queues work. (Also DFS if I replace the queue with a stack). It works, but I'm curious how well I implemented the algorithm. Are there any ...
1
vote
1answer
75 views

TLE in JNEXT problem in SPOJ

Problem Statement: JNEXT DevG was challanged to find the just next greater number which can be formed using digits of a given number. Now DevG needs your help to find that just next greater number ...
1
vote
2answers
47 views

Fisher-Yates Shuffle

This is a cut-and-dried Fisher-Yates implementation. As not typically a JS developer, I've tried to incorporate as many best practices as I can. I would appreciate any input as to make it conform to ...
5
votes
1answer
166 views

Determinant using Gauss Elimination

We had a question in examination to compute determinant of general square matrix. Now I had made the normal program using Laplace Formula of determinant, ie, recursive solution. Now I was thinking of ...
1
vote
2answers
82 views

Font and color checker chrome extension in Javascript

I have written the below code for a color and font checker on a webpage. The code is current and not so good. I would really like if I can get comments, reviews or any better approach for my PoC. ...
1
vote
2answers
57 views

When given 7 letters, find all possible words with length 3 to 7

I am writing a program in JavaScript to find all possible words given 7 letters. The words have to be 3 or more characters, and for right now I am limiting it to 7 characters long. The code below ...
2
votes
3answers
177 views

Addition of 2 integers

I've implemented an algorithm found in a book. It adds 2 positive integers of equal size. How can it be improved? It is assumed that inputted data is always positive integers with an equal number of ...