None Interview Question for Students


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

sum of the numbers from 1 to n is n(n+1)/2
sum of the squares of the numbers from 1 to n is n(n+1)(2n+1)/6
passing through the array adding numbers and squares gives us conditions on m - d and m^2-d^2, where m is the missing value and d the duplicate. Then it is just trivial to find both m and d. You do the math. :)
O(n) time and O(1) space

- Anonymous February 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Typical way to do it is sort the list first and iterate to find the dups and missing numbers. This is python solution without checking the integrity of the input list. For your homework you might wanna do the check.

def foo(l):
    missing = None
    dup = None
    l.sort()
    for i in range(len(l)-1):
        if l[i]==l[i+1]:
            missing = l[i]
        elif l[i]==l[i+1]-2:
            dup = l[i]+1
    return (missing, dup)

- Anonymous February 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort then check. This is python without checking input list's integrity.

def foo(l):
    missing = None
    dup = None
    l.sort()
    for i in range(len(l)-1):
        if l[i]==l[i+1]:
            missing = l[i]
        elif l[i]==l[i+1]-2:
            dup = l[i]+1
    return (missing, dup)

- C.C. February 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

python. just sort and find.

def foo(l):
    missing = None
    dup = None
    l.sort()
    for i in range(len(l)-1):
        if l[i]==l[i+1]:
            missing = l[i]
        elif l[i]==l[i+1]-2:
            dup = l[i]+1
    return (missing, dup)

- CC February 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n)

def findMissingAndRepeat(sequence):
    helperArray = [0 for x in range(len(sequence))]
    
    for num in sequence:
        helperArray[num-1] += 1
    
    for i in range(len(helperArray)):
        if helperArray[i] == 0:
            print '{} is missing'.format(i+1)
        if helperArray[i] == 2:
            print '{} is repeated'.format(i+1)

print findMissingAndRepeat([3, 1, 5, 8, 8, 7, 6, 2, 4]) # 8 repeated, 9 missing

- adam February 26, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

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.

Learn More

Resume Review

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.

Learn More

Mock Interviews

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.

Learn More