# Questions tagged [algorithms]

In mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning.

141 questions
Filter by
Sorted by
Tagged with
18k views

### What is O(...) and how do I calculate it?

Help! I have a question where I need to analyze the Big-O of an algorithm or some code. I am unsure exactly what Big-O is or how it relates to Big-Theta or other means of analyzing an algorithm's ...
837k views

### Which hashing algorithm is best for uniqueness and speed?

Which hashing algorithm is best for uniqueness and speed? Example (good) uses include hash dictionaries. I know there are things like SHA-256 and such, but these algorithms are designed to be secure,...
• 22.9k
11k views

### Should I keep investing into data structures and algorithms? [closed]

These days, I'm investing heavily in data structures and algorithms and trying to solve some programming puzzles. I'm trying to code and solve with Java and Clojure. Am I wasting my time? should I ...
• 4,543
67k views

### What methods are there to avoid a stack overflow in a recursive algorithm?

Question What are the possible ways to solve a stack overflow caused by an recursive algorithm? Example I'm trying to solve Project Euler problem 14 and decided to try it with a recursive algorithm....
• 837
6k views

### I'd like to write an "ultimate shuffle" algorithm to sort my mp3 collection

I'm looking for pseudocode suggestions for sorting my mp3 files in a way that avoids title and artist repetition. I listen to crooners - Frank Sinatra, Tony Bennett, Ella Fitzgerald etc. singing old ...
• 689
301 views

### What is the best algorithm for implementing a system with a complex execution path?

I am currently working on an implementation that based on a set of user configurations should output a final decision. The multiple configurations are evaluated several times at different stages of ...
• 141
18k views

### Is big-O really that relevant when working in industry?

In every interview I have been in, I have been quizzed on mathematical analysis of complexity, including big-O notation. How relevant is big-O analysis to development in industry? How often do you ...
8k views

### How to turn truth table into smallest possible if / else block

How can I take a truth table and turn it into a compacted if block? For instance, let's say I have this truth table where A and B are conditions and x, y and z are possible actions: A B | x y z -----...
• 655
20k views

### How important is studying algorithms and theory is to becoming a great programmer? [duplicate]

Possible Duplicate: Should I keep investing into data structures and algorithms? I'm a CS student. I want to become a really great programmer, what do I need to do to be come a great programmer? ...
• 349
11k views

### Big-O for nested loop

I am reading this post on Big-O It says that the following code is O(n^2): bool ContainsDuplicates(String[] strings) { for(int i = 0; i < strings.Length; i++) { for(int j = 0; j &...
• 1,830
10k views

### Unit-testing of inherently random/non-deterministic algorithms

My current project, succinctly, involves the creation of "constrainably-random events". I'm basically generating a schedule of inspections. Some of them are based on strict schedule constraints; you ...
• 22.2k
84k views

### General way to convert a loop (while/for) to recursion or from a recursion to a loop?

This problem is mainly focusing on the algorithm, maybe something abstract and more academic. The example is offering a thought, I wanna a generic way, so example is only used as to make us more ...
• 543
42k views

### What algorithm should I use to create an automatic staff scheduling feature?

Imagine a small local business (in my case a dog daycare) with a few dozen part-time employees . The goal is to automatically create weekly staff schedules. My question is about what algorithmic ...
• 872
5k views

### Please explain the statement that the function an+b belongs to O(n^2) and Θ(n)?

Let's say I have a linear function f(n)= an+b, what is the best way to prove that this function belongs to O(n2) and Θ(n)? I do not need mathematical rigor here. I need a programmers answer. Some ...
• 5,187
5k views

### Is it inefficient to concatenate strings one at a time?

I recall back from my days of programming in C that when two strings are joined, the OS must allocate memory for the joined string, then the program can copy all the string text over to the new area ...
• 601
2k views

### Match two strings but allow for a degree of error

How can I match two strings, but at the same time allow for X number of characters to be incorrect in the match. The number of errors should be a controllable variable. While X number of characters ...
• 13.1k
3k views

### Algorithm for an exact solution to the Travelling Purchaser Problem

do you know of any algorithms which give an exact solution for the Traveling Purchaser Problem. I can only find heuristic and probabilistic approaches. I do have implemented a genetic algorithm so ...
• 387
3k views

### Algorithm for mapping users to a schedule based on time availability

I have a project where I must map employees to a schedule. The basic concept is: Company Opens between 08:30 and 17:00. Employee 1 can work from 08:30 to 12:00 then from 13:00 to 1700; Employee 2 can ...
• 2,159
1 vote
2k views

### How to test an algorithm with multiple inputs?

I want to check group of algorithms which has up to 8 input options. The native idea would be to check all possibilities via brute force. How can I reduce it without leaving out required combinations? ...
• 113
1 vote
2k views

### Unevenly distributed random number generation [duplicate]

Let's say I have to generate random number from 1 to 100, however, the probability of each number is not 1/100, but a predefined probability. How to generate that number? I use Ruby/Python.
• 325
18k views

### Is it just me or is this a baffling tech interview question? [closed]

Background I was just asked in a tech interview to write an algorithm to traverse an "object" (notice the quotes) where A is equal to B and B is equal to C and A is equal to C. That's it. ...
• 3,315
14k views

### Simple method for reliably detecting code in text?

GMail has this feature where it will warn you if you try to send an email that it thinks might have an attachment. Because GMail detected the string see the attached in the email, but no actual ...
42k views

### Best practice/Patterns for two way data synchronisation

Quite often in my work the idea of 2-way data synchronisation between database systems crops up. The classic example is two slightly different CRM systems (say, Raiser's Edge and Salesforce) and the ...
• 809
55k views

### get weighted random item

I have, for example, this table +-----------------+ | fruit | weight | +-----------------+ | apple | 4 | | orange | 2 | | lemon | 1 | +-----------------+ I need to return a random ...
• 701
119k views

### What is your favorite whiteboard interview problem? [closed]

Just as the title says, what is your favorite whiteboard interview problem, and why has it proven effective for you? Junior, senior, Java, C, Javascript, PHP, SQL, pseudo-code, etc.
48k views

### What algorithm would you best use for string similarity?

I am designing a plugin to uniquely identify content on various web pages, based on addresses. So I may have one address which looks like: 1 someawesome street, anytown, F100 211 later I may find ...
• 511
38k views

### How do you identify "edge" cases on algorithms?

Basically how do you find out which could be your worst or best case and any other "edge" cases you might have BEFORE having them and so, how do you prepare your code for them?
• 635
6k views

### Can you use Pi as a crude random number generator?

I recently saw this question over at math.SE. It got me thinking. Could Pi be used as a crude random number generator? I mean the results are well known(how long has pi been computed to now?) but, Pi ...
• 22.9k
16k views

### How to implement a Weighted shuffle

I recently wrote some code that I thought very inefficient, but since it included only a few values, I accepted it. However, I'm still interested at a better algorithm for the following: A list of X ...
• 2,390
17k views

### Conflict resolution for two-way sync

How do you manage two-way synchronization between a 'main' database server and many 'secondary' servers, in particular conflict resolution, assuming a connection is not always available? For example, ...
• 4,505
2k views

### When to prefer a generalized solution over solving specific cases

In programming we're often faced with a choice: cover each conceivable use case individually, or solve the general problem: Its obvious that solving the immediate problem is faster, however creating ...
• 1,033
18k views

### Is it essential to learn algorithms to be a real programmer? [closed]

I'm a PHP programmer, and until now I have not needed to learn algorithms... Now I'm start learning Python (a real programming language), because I need to use matplotlib. Does it make sense to start ...
• 333
1k views

### What class of problem is this, and what math do I need to know to solve it?

Mushroom cultivation requires fairly precise chemical composition of substrate (a.k.a. growing medium). Let's pretend we're growing shitakes and that this is the required composition of their ...
• 1,091
11k views

### Algorithm for flattening overlapping ranges

I am looking for a nice way of flattening (splitting) a list of potentially-overlapping numeric ranges. The problem is very similar to that of this question: Fastest way to split overlapping date ...
• 321
4k views

### Algorithm for Learning development [closed]

This is a fairly general question. I know a bit of Perl and Python and I am looking to learn programming in more depth so that once I get the hang of it I can start developing applications and then ...
• 179
6k views

### Amortized Analysis? (Worst-case Performance Guarantees)

What is Amortized Analysis? And how can it help me achieve worst-case performance guarantees in my programs? I was reading that the following techniques can help the programmer achieve Worst-case ...
• 900
7k views

### Name of the Countdown Numbers round problem - and algorithmic solutions?

For the non-Brits in the audience, there's a segment of a daytime game-show where contestants have a set of 6 numbers and a randomly generated target number. They have to reach the target number using ...
• 680
8k views

I admit this was asked to me in interview a long time ago, but I never bothered to check it. The question was simple, how does Python make Queue thread-safe? My answer was, because of Interpreter ...
• 425
12k views

### Need help identifying a league scheduling algorithm

I am trying to create a sports league scheduler. I am having trouble identifying an algorithm to help me efficiently fill in each slot. Sample data to build the schedule would be: 10 teams Each ...
• 191
1k views

### Is it ethical to adopt other programmer's algorithms?

Imagine I find a completely new algorithm to compute the square root of a binary number (simplification of real scenario) on an internet forum that was an original creation. Is it ethical for me to ...
• 191
11k views

### Why is the "period of a (pseudo)random number generator" important?

I've been trying to understand (pseudo)random number generator code from various sources and the concept of the period continues to elude me. To satisfy the minimum level of understanding, I've tried ...
• 163
1k views

### Fuzzy algorithm for grouping data

I have data that indicates which topics each student has got right/wrong in an assessment.: | Student | Topic 1 | Topic 2 | Topic 3 | Topic 4 | Topic 5 | ... (perhaps 30 topics) | 1 | T | ...
• 345
2k views

### Search algorithm with co-ordinate (x,y) hints?

I am about to start writing a UI view where many small text items are laid-out over the view and when the user hovers over a text item a dynamically-generated image is displayed (like a tooltip), ...
• 205
6k views

### Algorithm to figure out appointment times?

I have a weird situation where a client would like a script that automatically sets up thousands of appointments over several days. The tricky part is the appointments are for a variety of US time ...
• 24k
8k views

### How to efficiently search a set of vectors for a vector which is the closest match

Unfortunately, I am not even sure of the keywords to use for this question, so if this has already been asked please point me the right way. Given a set of vectors: [3, 5, 3] [10, 23, 5] [123, 53, ...
• 147
2k views

### Fastest algorithm of dividing array into positive and negative numbers

Given array of integers I am trying to design the fastest algorithm that swaps the elements such that at the end : all negative elements are on the left and then the positive elements, for example, ...
1 vote
1k views

### How to convert this recursive problem to iterative? Line Simplification algorithm fails to run due to Maximum Recursion Depth being hit

I am implementing the Douglas, Peucker's Line Simplification algorithm in Python. I started with this implementation. However, it fails to run in Python due to Maximum Recursion Depth being hit. How ...
• 113
1 vote
496 views

### Algorithm to find the fewest number of values (from a set of values) where the sum equals a value (+ one more condition)

The simplest way to explain this is with an example. You're given then number 19 and you have a set of numbers to choose from: 1, 2, 3, 4, 6 When choosing values from the list (which can be ...
514 views

### How to Simulate Control-Flow without using Control-Flow Primitives

Basically, I want to know how to simulate while and if if I'm handling the control flow myself through an array of instructions. The while loop can be simulated by if, as seen with assembly branching ...
• 2,605