Facebook Interview Question for SDE1s


Country: United States




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

Lets start with a bit of math before coding,Imagine the number 897 and digit 6:
1) From 0 - 9 every digit comes once
2) From 0 - 99 every digit comes 20 times except 0 which comes 10 times
3) Now count(897, 6) = count(0-799, 6) + count(800-897, 6)
count(0-799, 6) = count(99, digit) * 8 + 100 since 6 <= 7 otherwise 0
count(800-897) = count(97, 6) + 6 == 8 ? 98 : 0
Once you get the formulae right its very simple dynamic programming with memorization:

public class DigitOccurence {
  public int getOccurence(int number, int digit) {
    number = Math.abs(number);
    if(digit < 0 || digit > 9) {
      return 0;
    }

    Map<Integer, Integer> cache = new HashMap<>();
    return this.getOccurence(number, digit, cache);
  }

  private int getOccurence(int number, int digit, Map<Integer, Integer> cache) {
    // Every digit appears once from 0 - 9
    if(number < 10) {
      return 1;
    }
    // Every digit appears 20 times from 0 - 99 except 0 which appears 10 times
    else if(number < 100) {
      return digit == 0 ? 10 : 20;
    }
    else if(cache.hasKey(number)) {
      return cache.get(number);
    }

    int numDigits = this.getNumDigits(number);
    int msd = this.getMostSygnificantDigit(number);
    int numButMsd = number - msd * Math.pow(10, numDigits - 1); // get 98 out of 898
    /**
    Here is the magic:
    => C(898, digit) = C(0-799, digit) + C(800-898, digit) //  msd is 8, numButMsd is 98
    => C(799, digit) = C(99, digit) * 8 + (digit < 8) ? 100 : 0
    => C(800-898, digit) = C(98, digit) + (digit == 8) ? 99 : 0;
    => C(898, digit) = (C(799, digit) = C(99, digit) * 8 + (digit <= 6) ? 100 : 0)
      + (C(98, digit) + (digit == 8) ? 99 : 0);
    */
    int occurence = (msd * this.getOccurence(Math.pow(10, numDigits - 1) - 1, digit, cache) + (digit < msd) ? Math.pow(10, msd - 1) : 0)
      + (this.getOccurence(numButMsd, digit, cache) + digit == msd ? numButMsd + 1 : 0);
    cache.put(number, occurence);
    return occurence;
  }

  private int getNumDigits(int number) {
    int count = 0;
    while(number > 0) {
      count++;
      number = number / 10;
    }
    return count;
  }

  private int getMostSygnificantDigit(int number) {
    if(number == 0) {
      return 0;
    }
    int numDigits = this.getNumDigits(number);
    return number / Math.pow(10, number - 1);
  }
}

- nk June 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int getNextPrime(int n)
{
int c=2;
for(int i=n+1; ;i++)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
c++;
if(c>3)
{
break;
}
}
if(c==2)
{
return i;
}
c=2;
}
}
int main()
{
int n;
int t=2;
int a[100],i=0;
cin >>n;
while(n!=0)
{
if(n%t==0)
{
n=n/t;
a[i]=t;
i++;
}
else
{
t=getNextPrime(t);
}
}
for(int j=0;j<=i;j++)
{
cout << a[j]<<"*";
}
}

- Rajesh Sharma June 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in python. Cost O(nLogn).
Log n is the cost of computing the number of 3 at a given number (we divide the number by 10 each time) and we perform this operation n times.

def numof3(n):
    total = 0
    while (n > 10):
        if (n % 10) == 3: total += 1
        n /= 10
    if n == 3: total += 1
    return total
    
def count3(n):
    total = 0
    for x in xrange(1, n+1):
        total += numof3(x)
    return total

- Fernando June 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for every number from 1 to N count number of digits = K

implementation in Scala:

object NumKCount {
  
  def main(args: Array[String]): Unit = {        
    println(countK(30,3))
  }

  def countKDigit(n:Int, k:Int):Int = {
      var num = n
      var count = 0
      while (num > 10) {
        val digit = num % 10
        if (digit == k) {count += 1}
        num = num / 10
      }
      if (num == k) {count += 1}                       
      count
  }
  
  def countK(n:Int, k:Int):Int = {          
    1.to(n).foldLeft(0)((acc, x) => acc + countKDigit(x, k))
  }
}

- guilhebl June 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var number = 2350;
  for(i=1;i<=number;i++){
    hasThree(i, i);
  }

  function hasThree(n, original) {
    if(n > 0 && n%10 == 3) { 
	  console.log(original);
    } else if(n > 0) {
      hasThree(Math.floor(n/10), original);
    }

}

- jaylu June 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

def countOccurences(x, y):
    q = Queue.Queue(x);
    q.put(y);
    count = 0;
    visited = [];
    while not q.empty():
          item = q.get();
          count = count + str(item).count(str(y));
          for i in range(0, 10):
              temp = str(item) + str(i);
              if int(temp) <= x and temp not in visited:
                  q.put(temp);
                  visited.append(temp);
          for i in range(1, 10):
              temp = str(i) + str(item);
              if int(temp) <= x and temp not in visited:
                  q.put(temp);
                  visited.append(temp);        
    print count;

countOccurences(40,4);

- koustav.adorable June 21, 2017 | 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