Palantir Technology Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

package com.mirak.careercup.palantir;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DecodeGrid {

  private class Pair {
    char c;
    int count;

    Pair(char c, int count) {
      this.c = c;
      this.count = count;
    }
  }

  public List<String> decodeGrid(String grid) {
    ArrayList<Pair> list = new ArrayList<>();
    Set<Character> validChars = new HashSet<>();
    validChars.add('_');
    validChars.add('r');
    validChars.add('b');

    int rows = decodeGridToChars(grid, list, validChars);

    int n = findLeastDivisor(rows);
    List<String> decodedGrid = new ArrayList<>();
    StringBuilder builder = new StringBuilder();
    int takenColumns = 0;
    for (int i = 0; i < list.size(); i++) {
      Pair pair = list.get(i);
      for (int j = 0; j < pair.count; j++) {
        builder.append("|").append(pair.c);
        takenColumns++;
        if (takenColumns == n) {
          pair.count -= (j + 1);
          takenColumns = 0;
          decodedGrid.add(builder.append("|").toString());
          builder = new StringBuilder();
          i--;
          break;
        }
      }
    }
    return decodedGrid;
  }

  private int findLeastDivisor(int len) {
    for (int i = 2; i * i <= len; i++) {
      if (len % i == 0) {
        return i;
      }
    }
    return 1;
  }

  private int decodeGridToChars(String grid, ArrayList<Pair> list, Set<Character> validChars) {
    int len = 0;
    int idx = 0;
    while (idx < grid.length()) {

      if (Character.isDigit(grid.charAt(idx))) {
        int j = idx + 1;
        while (j < grid.length() && Character.isDigit(grid.charAt(j))) {
          j++;
        }

        if (j == grid.length() || !validChars.contains(grid.charAt(j))) {
          return -1;
        }
        len += Integer.parseInt(grid.substring(idx, j));
        list.add(new Pair(grid.charAt(j), Integer.parseInt(grid.substring(idx, j))));
        idx = j + 1;
      } else {
        if (!validChars.contains(grid.charAt(idx))) {
          return -1;
        }
        list.add(new Pair(grid.charAt(idx), 1));
        idx++;
        len++;
      }
    }
    return len;
  }
}

- mirak94 June 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.home.careercup;

public class DecodeCFNString {

    public static void main(String[] args) throws Exception {

        String in = "9_r4_brbrbr_3b2rb_b2r2br_r2b3rb";
        char[][] result = decode(in);
        for (char[] s : result) {
            System.out.println(s);
        }
    }

    static char[][] decode(String cfn) throws Exception {
        char[][] result = new char[6][7];
        char[] code = cfn.toCharArray();
        StringBuilder sb = new StringBuilder();
        int expand = 0;

        for (char c : code) {
            if (expand > 0) {
                for (; expand > 0; expand--)
                    sb.append(c);
                continue;
            }

            switch (c) {
                case 'r':
                    sb.append(c);
                    break;
                case 'b':
                    sb.append(c);
                    break;
                case '_':
                    sb.append(c);
                    break;
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case '0':
                    expand = (int) c - '0';
                    break;
                default:
                    throw new Exception("invalid cfn string, found char " + c);
            }
        }
        // now we need to end up with a 7*6 length char string
        if (sb.toString().length() != 7 * 6) throw new Exception("invalid cf string");
        final String rs = sb.toString();

        for (int i = 0, start = 0, end = 7; end <= 42; i++, start = end, end += 7) {
            result[i] = rs.substring(start, end).toCharArray();
        }
        for (char[] s : result) {
            validate(s);
        }
        return result;
    }

    private static void validate(char[] s) {
        //TODO:
        //throw exception if the validation does NOT succeed
        // for example there may be  a digit that is placed
        // such that the number of spaces to its right does
        // not justify its value
        return;
    }
}

- Makarand September 02, 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