Algorithms interview questions [duplicate] - algorithm

This question already has answers here:
Algorithm interview from Google
(8 answers)
Closed 8 years ago.
I am a long time lurker, and just had an interview with Google where they asked me this question:
Given a requested time d which is impossible (i.e. within 5 days of an already scheduled performance), give an O(log n)-time algorithm to find the next available day d2 (d2 > d).
I had no clue how to solve it, and now that the interview is over, I am dying to figure out how to solve it. Knowing how smart most of you folks are, I was wondering if you can give me a hand here. This is NOT for homework, or anything of that sort. I just want to learn how to solve it for future interviews. I tried asking follow up questions but he said that is all I can tell you.
Thanks!

This is completely firing from the hip because I'm not sure if the question is complete, but if you had a list of dates in an array such that d[0] < d[1] < ... < d[n], the simple answer would be a binary search tree to find the next day.

Related

What is the best algorithm for optimizing a combination of variables? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 20 hours ago.
Improve this question
I want to program a software that calculates the best combination of materials to use base on parameters such as its tensile strength, elastic modulus, stiffness, and results from doing certain tests from those materials. Those each factor are going to be weighted differently in a WDM. Is there an algorithm that would allow me to find the best combination without actually going through all the combinations and doing each individual calculations? I will be working with a lot of data, so efficiency is important
I tried researching algorithms like kruskal's and other things, but I'm not very fammiliar with them
First step is to write down an equation to calculate a number that you want to optimize.
If you can do that and the equation has no squares or other exponential terms then this is the classical linear programming problem https://en.wikipedia.org/wiki/Linear_programming
Your equation needs to look something like this:
max O = n1 * p1 + n2 * p2 - n3 * p3 ...
If so, then your best bet is to choose a linear programming package ( ask google ) with a good introductory tutorial and plug your problem into that. After a day or so on a steep learning curve, your problem will become almost trivial.
If you cannot do that, then you will need to use some sort of hill climbing algorithm - probably best to hire an expert to help with that.

Algorithm for solving "Pipes" Game [duplicate]

This question already has answers here:
Algorithm for solving Flow Free Game
(6 answers)
Closed 7 years ago.
I'm looking to create a program to perform a puzzle for me in a similar fashion as this one. I have completed the piece searching portion to identify each of the pieces but I'm stuck at where to being to start solving the puzzle.
Backtracking is an option but for very large board sizes it will have high complexity and take time.
Is there any suitable algorithm to solve the game efficiently. Can using heuristics to solve the board help?
Looking for a direction to start.
It's hard to understand the precise logic of the game you posted. However, if you want something more efficient than DFS/BFS, then you should take a look at A* star: http://theory.stanford.edu/~amitp/GameProgramming/

Permutation of a numbers in array to sum a number [duplicate]

This question already has answers here:
How to count possible combination for coin problem
(18 answers)
Closed 8 years ago.
I have given an array.And i want to find the all permutation of an array so it sum to a specific numbers.ExampleArray a =[2,3,5 ,1] Target = 8`Solution: [2,2,2,2] ,[5,3] ,[3,3,2] ,[5,2,1] and all possible combinationPlease provide me a approach to solve this the problem , the problem i am facing how to handle the repetition of the elements.Target is a large number of 10^6.
I think it is same asThis theory
You are facing a typical Subset Problem. The worst case complexity of this problem is exponential no matter how you put it. You might find good polynomial-time approximations that work wonders for average case though.

Do we really have case in this algorithm [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having trouble for solving the running time of the following algorithm
Now first my question, is the case really important here(I can not come up with 2 different inputs of the same size that are different from each other) ?
Second, I think this algorithm runs in O(n^2). Am I right?
The comment you wrote in #OBu's answer is about only a quarter right:
1*n + 2*(n-1) + 3*(n-2) + ... +n*1
That equals to:
Sum(i=1..n, i*(n-i+1)) = n*Sum(i) - Sum(i*i) + n = n*[n(n+1)/2] - [n(n+1)(2n+1)/6] + n
If you want, feel free to compute the exact formula, but the overall complexity is O(n^3).
As a rule of thumb (more like a back-of-the-envelope computation trick I've picked up... just to give you a quick idea): if you are unsure about algorithms with multiple for's (with different lengths, but all in relation with n, as you have above) try to compute how many operations are performed around the middle of the algorithm (n/2). This usually gives you an idea on how the running time complexity for the whole thing might looks like - you are basically computing the largest element in the sum, so the overall complexity is always >= than the thing you compute (in most cases it's the same though).
Just to give you some hints:
How many loops do you have and how are they nested?
How often is each loop running (start solving this from the outer to the inner loop)
If in doubt, try it with n=4 or 5 and calculate each step. After this, you'll have a good feeling for what's going on.

Problems and with which algorithm technique they can be solved? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm looking for a list of problems (the more the problems the better) with their corresponding algorithm technique(s) these problems can be solved.
For example:
Shortest Path problem -> Dijkstra's algorithm (dynamic programming), and maybe others ...
Knapsack problem -> can be solved with dynamic programming, ...
Convex Hull problem -> could be done by divide and conquer, ...
If in every problem there is a small paragraph explaining how the problem is solved (e.g. it could explain how to "divide" the problem in a divide and conquer algorithm) with the given
technique that would be even better.
Is there any such list available in the net or in a book?
*UPDATE after locking ... *
I'm not looking for problems mostly found in programming contests like TopCoder. Problems from such sites (contests) use to "hide" the problem definition which is supposed to be found by the user. I'm looking for problem definitions, like Given a graph G = (V, E) ... and it's given solution technique Could be solved using divide and conquer ... instead of problems definitions like this: Given N houses, John tries to find a path to go home as fast as possible ... which are how problems are described in programming contests sites.
This question has nothing to do with homework as some people thought. I want to "exercise" my algorithm solution technique skills. By knowing that a given problem can be solved by a given technique will help me try to find a solution using this technique and so I guess will get a deeper understanding of each technique, plus I will be become a better algorithm solver.
The site Algorithmist categories many problems (but not all) from UVa online judge and Sphere Online Judge. For example, here is the partial category listing for UVa Volume I. Also check their Categories page.
Topcoder categorizes each problem from every past contest they held. Besides that, you can filter problems by difficulty, as well as read editorials or other people's solutions.

Resources