Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [algorithm]

An algorithm is a sequence of well-defined steps that defines an abstract solution to a problem. Use this tag when your issue is related to algorithm design.

5379 votes
43 answers
836k views

What is a plain English explanation of "Big O" notation?

I'd prefer as little formal definition as possible and simple mathematics.
Arec Barrwin's user avatar
  • 61.8k
4951 votes
62 answers
3.6m views

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++...
brad's user avatar
  • 75.1k
4201 votes
38 answers
441k views

How can I pair socks from a pile efficiently?

Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. I was doing a naive search — picking one sock and "iterating" the pile in ...
amit's user avatar
  • 178k
2744 votes
32 answers
1.6m views

What does O(log n) mean exactly?

I am learning about Big O Notation running times and amortized times. I understand the notion of O(n) linear time, meaning that the size of the input affects the growth of the algorithm ...
Andreas Grech's user avatar
2071 votes
15 answers
1.0m views

What is the optimal algorithm for the game 2048?

I have recently stumbled upon the game 2048. You merge similar tiles by moving them in any of the four directions to make "bigger" tiles. After each move, a new tile appears at random empty position ...
nitish712's user avatar
  • 19.7k
2060 votes
30 answers
619k views

What is tail recursion?

Whilst starting to learn lisp, I've come across the term tail-recursive. What does it mean exactly?
1913 votes
23 answers
242k views

Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

One of the most interesting projects I've worked on in the past couple of years was a project about image processing. The goal was to develop a system to be able to recognize Coca-Cola 'cans' (note ...
Charles Menguy's user avatar
1677 votes
22 answers
300k views

What is the best algorithm for overriding GetHashCode?

In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when ...
bitbonk's user avatar
  • 49.3k
1481 votes
57 answers
2.2m views

Removing duplicates in lists

How can I check if a list has any duplicates and return a new list without duplicates?
Neemaximo's user avatar
  • 20.6k
1282 votes
49 answers
326k views

Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing

I had an interesting job interview experience a while back. The question started really easy: Q1: We have a bag containing numbers 1, 2, 3, …, 100. Each number appears exactly once, so there are 100 ...
polygenelubricants's user avatar
1233 votes
7 answers
210k views

Ukkonen's suffix tree algorithm in plain English

I feel a bit thick at this point. I've spent days trying to fully wrap my head around suffix tree construction, but because I don't have a mathematical background, many of the explanations elude me as ...
Nathan Ridley's user avatar
1167 votes
50 answers
1.2m views

Calculate distance between two latitude-longitude points? (Haversine formula)

How do I calculate the distance between two points specified by latitude and longitude? For clarification, I'd like the distance in kilometers; the points use the WGS84 system and I'd like to ...
Robin Minto's user avatar
  • 15.3k
1107 votes
10 answers
300k views

What is tail call optimization?

Very simply, what is tail-call optimization? More specifically, what are some small code snippets where it could be applied, and where not, with an explanation of why?
majelbstoat's user avatar
  • 13.2k
1042 votes
10 answers
883k views

How can I find the time complexity of an algorithm?

I have gone through Google and Stack Overflow search, but nowhere I was able to find a clear and straightforward explanation for how to calculate time complexity. What do I know already? Say for code ...
Yasser Shaikh's user avatar
1016 votes
66 answers
654k views

Count the number of set bits in a 32-bit integer

8 bits representing the number 7 look like this: 00000111 Three bits are set. What are the algorithms to determine the number of set bits in a 32-bit integer?

15 30 50 per page
1
2 3 4 5
8072