Stack Exchange Network

Stack Exchange network consists of 175 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
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

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
5 views

Determine whether array could become non-decreasing by modifying at most 1 element

The task Given an array of integers, write a function to determine whether the array could become non-decreasing by modifying at most 1 element. For example, given the array [10, 5, 7], you ...
5
votes
1answer
35 views

JavaFX Connect Four AI

I made a Connect Four AI with minimax algorithm. It's my first bigger JavaFX project. Any help for improvements would be really appreciated. Board: ...
1
vote
0answers
28 views

Find non-duplicated integer in a list where every integer occurs three times except for one integer

The task Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer. For example, given [6,...
3
votes
1answer
56 views

Division without using division, multiplication, or modulus operators

The task Implement division of two positive integers without using the division, multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder. My solution <...
2
votes
1answer
68 views

Implement Queue using fixed size array

I came across below interview question and I am working on it: Build a queue class with the enqueue and dequeue methods. The queue can store an UNLIMITED number of elements but you are limited to ...
1
vote
2answers
42 views

Sum of two numbers in array

The task You are given two non-empty arrays representing two non-negative integers. The digits are stored in reverse order and each of their items contain a single digit. Add the two numbers ...
-3
votes
0answers
42 views

What's the complexity of the algorithm to find median of 2 sorted arrays [migrated]

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be \$O(log(𝑚+𝑛))\$ Here is the code that I ...
1
vote
1answer
40 views

Find busiest period in building

The task You are given a list of data entries that represent entries and exits of groups of people into a building. An entry looks like this: {"timestamp": 1526579928, count: 3, "type": "...
0
votes
1answer
35 views

Making Anagram Problem Implementation

Problem Statement Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged ...
1
vote
1answer
47 views

MinMax Problem Implementation

Problem Statement: You will be given a list of integers, arr, and a single integer k. You must create an array of length k from elements of arr such that its unfairness is minimized. Call ...
0
votes
1answer
40 views

Find indices of two numbers such that they add up to a target

The task ...is taken from leetcode Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have ...
2
votes
0answers
38 views

Code Chef's Cleaning Tables: find the minimum number of tables to clean

Description: Haku has been newly hired by Chef to clean tables at his restaurant. So whenever a customer wants a table, Haku must clean it. But Haku happens to be a lazy boy. So in the ...
1
vote
0answers
36 views

Find all pairs of unique indices in a list such that the concatenation of the two words is a palindrome

The task Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome. For example, given the list ["code", "edoc", "da", "d"], return ...
0
votes
0answers
47 views

Codility Ladder (Fibonacci) lesson [on hold]

I am trying to solve the "Ladder" problem from Codility. I got only 37% score (75% for correctness and 0% for performance). Can anyone help to identify the problem in my code. I found an optimal ...
1
vote
1answer
44 views

Rotate a N by N matrix by 90 degrees clockwise

The task: Given an N by N matrix, rotate it by 90 degrees clockwise. For example, given the following matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] you should return: [[7, 4, 1], [8, ...
1
vote
1answer
41 views

Check whether string of brackets are well-formed

The task I asked similar questions here and here. Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed). For example, ...
9
votes
5answers
240 views

Sort a list of pairs representing an acyclic, partial automorphism

I have this sample data: let trips = [ { from: "DEN", to: "JFK" }, { from: "SEA", to: "DEN" }, { from: 'JFK', to: 'SEA' }, ]; ...
3
votes
3answers
56 views

Implement integer exponentiation faster than repeated multiplication

The task: Implement integer exponentiation. That is, implement the pow(x, y) function, where x and y are integers and returns x^y. Do this faster than the naive method of repeated ...
4
votes
2answers
74 views

Check whether string of braces, brackets, and parentheses is balanced

The Task is taken from codewars: Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if ...
1
vote
2answers
96 views

Leetcode: Roman digit to integer

I tried to solve the question at https://leetcode.com/problems/roman-to-integer/ i.e Converting an Roman Digit to a Integer using C#. It passes all the test cases but any suggestions to reduce run ...
1
vote
1answer
77 views

Sum of number's quotients when divided by exact powers of 2

Problem I have written code to solve this challenge: Given a formula: GCD(x,y) means the GCD (Greatest Common Divisor) of x and y. For example: if N=2, then: f(2) = 1/GCD(1,...
1
vote
0answers
76 views

HackerRank Equal Solution [closed]

Source: https://www.hackerrank.com/challenges/equal/problem Problem Description: Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. She is ...
1
vote
2answers
62 views

Minimum number of parentheses to be removed to make a string of parentheses balanced

The task: Given a string of parentheses, write a function to compute the minimum number of parentheses to be removed to make the string valid (i.e. each open parenthesis is eventually closed). ...
2
votes
1answer
49 views

Speed up fast poisson disk sampling generator in Python

I wrote a generator class which implements Fast Poisson Disk Sampling algorithm in Python. I did some optimizations like, x ** 2 -> x * x, using unpacking ...
1
vote
1answer
44 views

Return a new array where each element is the number of smaller elements to the right of that element in the original input

The task: Given an array of integers, return a new array where each element in the new array is the number of smaller elements to the right of that element in the original input array. For ...
6
votes
2answers
75 views

Find duplicate in linear time and space

The task: You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in linear time and space. <...
4
votes
1answer
33 views

Code to find if a number fulfills certain requirements (e.g. sequential increasing, palindrome, repeating, etc)

I'm currently a 3rd year computer science student and lecturers have never checked the readability and maintainability of code. We were only marked on our outputs and as such I've written some truly ...
3
votes
2answers
99 views

Trip planning algorithm

I was presented with an interview question described as follows: Receiving an int[] A of cities, where each A[i] has an appeal ...
2
votes
0answers
58 views

Return whether a string is a palindrome if you can delete at most k elements

The task: Given a string which we can delete at most k, return whether you can make a palindrome. For example, given 'waterrfetawx' and a k of 2, you could delete f and x to get '...
1
vote
0answers
52 views

Rotate a list by k elements

The task: Write a function that rotates a list by k elements. For example, [1, 2, 3, 4, 5, 6] rotated by two becomes [3, 4, 5, 6, 1, 2]. Try solving this without creating a copy of the list. ...
5
votes
3answers
106 views

Repeatedly keep removing 2 smallest elements and add its sum back to the list and return newly inserted elements' total

I have a list of unsorted numbers. Each time, I need to remove 2 smallest numbers and insert back its sum into the list. Repeatedly do this till the 1 final element is remaining in the list. Return ...
4
votes
2answers
251 views

Possibly bubble sort algorithm

I'm trying to figure out what to call this sorting algorithm: ...
3
votes
0answers
45 views

Array Search Algorithm

Recently, I've been experimenting with an array searching algorithm that I'm not sure I've seen anywhere (minor references here). I'm still not fully-learned on how to state the efficiency of what I'...
1
vote
0answers
55 views

Sort a list using a reverse helper function

The task: Given a list, sort it using this method: reverse(lst, i, j), which reverses lst from ...
1
vote
2answers
55 views

Find the smallest distance between any two given words in a string

The task: Find an efficient algorithm to find the smallest distance (measured in number of words) between any two given words in a string. For example, given words "hello", and "world" and a ...
4
votes
1answer
176 views

Best way to performance permutations

This has been my white whale for a while, the idea is convert all the symbols to the goal symbol using all the pieces. I did an algorithm like 5 years ago (now is lost) and reach lvl 48 but got stuck ...
1
vote
1answer
58 views

Leetcode online Find the judge, graph with highest degree

Here is the problem. In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town ...
0
votes
0answers
40 views

Kadane's algorithms to leetcode “121 Best Time to Buy and Sell Stock”

I employed the conventional Kadane's algorithms to solve a maximum subarray problem in leetcode Best Time to Buy and Sell Stock - LeetCode Best Time to Buy and Sell Stock Say you have ...
2
votes
3answers
115 views

Simple exchange calculator in Java - structure and tests

I am trying to learn how to correctly structure my projects and write a good, clean code. I would also like to learn how to write good tests. Can you please review it and give me some tips and tricks ...
2
votes
2answers
107 views

Consecutive words match in a dataset

I have written an algorithm to do consecutive word match in a dataset The input is a dataset (list of textlines) and I want to scan that and return repeated word(s)...
5
votes
1answer
98 views

Return the first recurring character in a string

The task: Given a string, return the first recurring character in it, or null if there is no recurring character. For example, given the string "acbbac", return "b". Given the string "...
4
votes
1answer
71 views

Determine whether any permutation of a string is a palindrome

The task: Given a string, determine whether any permutation of it is a palindrome. For example, "carrace" should return true, since it can be rearranged ...
2
votes
1answer
37 views

Kahn's algorithms to arrange CouseSchedule

I wrote a solution to Course Schedule II - LeetCode There are a total of n courses you have to take, labeled from 0 to n-1. ...
2
votes
3answers
100 views

Looking for all numbers where each digit is bigger than the previous digit

This is the "Increasing Number problem", where we are looking for all numbers with \$n\$ digits where each digit is bigger than the digit before it. Or in Mathematical therms where: $$\mathit{...
2
votes
2answers
104 views

PHP - Packing widgets into the fewest number of boxes, plus minimum order quantity

The problem is this: A company supplies widgets in a set of pack sizes: 250 500 1000 2000 5000 Customers can order any number of widgets, but the following rules apply: Only whole packs can be ...
4
votes
1answer
72 views

Time(n) Complexity of Fibonacci Algorithms

It is my first use of classes in Python. I wanted to create something useful and try some new things as I learn this cool language: strategy pattern, testing with nanoseconds precision, curve/poly ...
10
votes
3answers
1k views

Find the majority element, which appears more than half the time

The task: Given a list of elements, find the majority element, which appears more than half the time (> floor(len(lst) / 2.0)). You can assume that such element exists. For example, ...
5
votes
2answers
60 views

A recursive solution to symmetirc tree

I tried to solve a symmetric tree problem Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree ...
4
votes
1answer
110 views

Swedish 'Personnummer' validator

I have a small C# program that validates Swedish social security numbers ('Personnummer'). I chose to use Regex as there are many different ways it can be input. ...
11
votes
5answers
787 views

Easy to read palindrome checker

I made a palindrome checker that's supposed to be designed to be simple and easy to read. Please let me know what you think. I believe the time complexity is \$\mathcal{O}(n)\$ but I'm not too sure ...