Interview Question


Country: CHINA
Interview Type: In-Person




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

This P(x) can be written as ((...((cn-1)x +cn-2)x + cn-3)x + ... c2)x + c1
Assuming V is the vector/array holding the coefficients.

int y=0;
for(i=n-1; i>=0; i)
    y = V[i] + x.y;

The above way of evaluating polynomial is also known as Horner's rule.

- pavi.8081 August 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In above I have assumed x and Vector V to be integers.
Its trivial to extend it to float/ double

- pavi.8081 August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Typo correction:

for(i=n-1; i>=0; i++)

- pavi.8081 August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

typo correction...sorry for the mistake yet again :P

for(i=n-1; i>=0; i--)

- pavi.8081 August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i m not sure we need to calculate the value..i think we need to gen the expression which can be done using linked list....

- atul gupta August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The vector is already a compact form of the polynomial, so simply iterate over it and sum the results.

Optimization: Horner's rule is known to sum polynomials in O(n) running, more efficient than plain summing.

- Yev August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for i in range(1, n):
    C += C[i] * x^(n-1)

- daydreamer September 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The following is my code:
any bug/error exist?

public class construct_polynomial {

public static void polynomial(Vector<Double> v, double x){
double sum = 0.0;

for(int i = 0 ; i<=v.size()-1 ; i++){
sum += v.elementAt(i) * Math.pow(x,i);
}
System.out.println(sum);
}
}

- Xiaonb August 13, 2012 | 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