is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.
CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.
Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.
Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.
The question is not clear to me.
- arpmon October 15, 2014I can deduce 2 scenarios from the question:
Case 1: One number is repeated and the there are 'n+1' numbers.
Case 2: One number is repeated and another number is missing. So, there are 'n' numbers.
-----------------------
Case 1 Solution:
Calculate sum of available 'n+1' numbers. Let's say it's 's1' .
Calculate sum of (1, 2, ..., n) using formula n*(n+1)/2. Let's say it's 's' .
So, repeated number is (s1 - s)
Time complexity: O(n) i.e. time needed to traverse the array to calculate the sum.
-----------------------
Case 2 Solution:
Let's say that the missing number is 'a' and repeated number is 'b' .
Calculate sum of available 'n' numbers. Let's say it's 's1' .
Calculate sum of (1, 2, ..., n) using formula n*(n+1)/2. Let's say it's 's' .
If 's' is bigger than 's1', then (a - b) = (s - s1), otherwise (b - a) = (s1 - s) .
Calculate sum of squares of available 'n' numbers. Let's say it's 't1' .
Calculate sum of (1^2, 2^2, ..., n^2) using formula n*(n+1)*(2*n+1)/6. Let's say it's 't' .
If 't' is bigger than 't1', then (a^2 - b^2) = (t - t1), otherwise (b^2 - a^2) = (t1 - t) .
Now, (a^2 - b^2) = (a+b)*(a-b)
Hence, solve the two equations to get the missing number and repeated number.
Time complexity: O(n) i.e. time needed to traverse the array to calculate the sums.