Interview Question


Country: India




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

The answer depends on how frequently would you query for x being a sum of powers.

- Subbu. December 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you elaborate on this? How does it matter, how would you use that information?

- Anonymous December 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If lots of queries come, then it might pay to preprocess once and then do O(1) query times.

If very few queries come, them we might just brute force for each one.

- Subbu. December 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I solved it by storing all numbers from 1 to x, which could be obtained by raising numbers to the different powers greater than 1. Then just loop through all numbers between 1 and x and check if k and (x - k) are stored as powers of some other numbers.

- Alex December 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

time complexity: O(x * log x)
memory complexity: O(x)

void solve_equation(int x, int &a, int &b, int &n, int &m)
{
  a = b = n = m = 0;
  // powers[i] == (d, p) iff d^p == i
  vector < pair < int, int > > powers(x + 1, make_pair(0, 0));
  powers[1] = make_pair(1, 2);
  for (int i = 2; i * i <= x; ++i)
  {
    int d = i;
    int p = 1;
    while (d * i <= x)
    {
      d = d * i;
      p++;
      powers[d] = make_pair(i, p);
    }
  }
  
  for (int i = 1; i <= x; ++i)
    if (powers[i].first != 0 && powers[x - i].first != 0)
    {
      a = powers[i].first;
      n = powers[i].second;
      b = powers[x - i].first;
      m = powers[x - i].second;
    }
}

- Alex December 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One improvement , To detect if k is a perfect you can check for prime divisor of m till M<log2k

- Prithvi December 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The comment has been deleted

- loveCoding December 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would first do some preprocessing in order to get a set of numbers that are perfect powers less than 1000000. Start from i=2. For each i, keep raising its power till we exceed 1000000, while adding the number to the set. Note that we can stop at 1000 because 1000^2 = 1000000 already. Also note that 2^20 > 1000000, which means this preprocessing will take at most 20*1000 = 20000 operations.

After the preprocessing, we have a set that's <20000 elements. To check whether N can be expressed as the sum of 2 perfect powers, we can simply iterate through the set and check whether N - "current element" is also in the set.

- Sunny December 04, 2013 | 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