Facebook Interview Question for Software Engineers


Country: United States




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

do manual division and if the remainder reapears the pattern will repeat. The code below will put the repeating sequence in "()" e.g. 1/3 = 0.(3), 4 / 3 = 1.(3), 1/6 = 0.1(6)

public String fractionToDecimal(int numerator, int denominator) {
    StringBuilder sb = new StringBuilder();
    if((numerator < 0) ^ (denominator < 0)) sb.append("-");
    long num = Math.abs((long)numerator);
    long den = Math.abs((long)denominator);
    long rem = num % den;
    sb.append(num / den);
    if(rem > 0) {
        sb.append(".");
        HashMap<Integer, Integer> seenRemDigitIdx = new HashMap<>();
        while(rem != 0) {
            int lastSeen = seenRemDigitIdx.getOrDefault((int)rem, -1);
            if(lastSeen != -1) {
                sb.insert(lastSeen, '(');
                sb.append(')');
                break;
            }
            seenRemDigitIdx.put((int)rem, sb.length());
            rem *= 10;
            sb.append(rem / den);
            rem = rem % den;
        }
    } else if(sb.toString().equals("-0")) {
        return "0";
    }
    return sb.toString();
}

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

public class Solution {
  public String getRepetitivePattern(int dividend, int divisor) {
    if(divisor == 0 || dividend % divisor = 0) {
      return null; // There is no pattern
    }
    dividend = Math.Abs(dividend);
    divisor = Math.Abs(divisor);
    dividend = (dividend % divisor) * 10;
    StringBuilder sb = new StringBuilder();
    // Map of dividend to starting index of pattern
    Map<Integer, Integer> cache = new HashMap<>();
    int newDividend = dividend, quotient = 0;
    // Return null in case we can never find a pattern
    while(cache.size() < Integer.MAX_VALUE) {
      quotient = (int)(dividend / divisor);
      if(cache.contains(newDividend)) {
        return sb.substring(cache.get(newDividend));
      }
      if (!cache.contains(newDividend)) {
        cache.put(newDividend, substring.length());
        substring.append(quotient);
      }
      newDividend = (dividend % divisor) * 10;
    }
    return null;
  }
}

- nk June 13, 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