Koolaidrain

FB Questions

Nov 17th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. FB Production Engineering Interview Question and Code:
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. /*
  7. * To execute Java, please define "static void main" on a class
  8. * named Solution.
  9. *
  10. * If you need more classes, simply define them inline.
  11. */
  12.  
  13. // dataset1.csv
  14. // NAME,LEG_LENGTH,DIET
  15. // T-Rex,1.3,carnivore
  16.  
  17. // dataset2.csv
  18. // NAME,STRIDE_LENGTH,WALK_TYPE
  19. // T-Rex,3.7,bipedal
  20.  
  21. // Output in sorted order the name and speed of all bipedal dinosaurs.
  22.  
  23. public static final double g = 9.8;
  24.  
  25. class Dinosaur {
  26.  
  27. public class DiNode {
  28. String name;
  29. double stride_len;
  30. double leg_len;
  31. double speed;
  32.  
  33. public DiNode(String name, double stride_len) {
  34. this.name = name;
  35. this.stride_len = stride_len;
  36. this.speed = Integer.MIN_VALUE;
  37. }
  38.  
  39. public calculate_speed() {
  40. this.speed = ((stride_len/leg_len)/g);
  41. }
  42.  
  43. public toString() {
  44. String s = "";
  45. s += this.name;
  46. s += this.speed;
  47. return s;
  48. }
  49.  
  50. }
  51.  
  52.  
  53. public static void main(String[] args) {
  54.  
  55. try {
  56.  
  57. HashMap<String, DiNode> map = new HashMap<String, DiNode>();
  58. ArrayList<DiNode> list = new ArrayList<DiNode>();
  59. Scanner sc = new Scanner(new File("dataset2.csv"));
  60. String[] line;
  61. DiNode node;
  62.  
  63. while (sc.hasNext()) {
  64. line = sc.next().split(",");
  65. // Error checks for args
  66. if (line[2].equals("bipedal")) {
  67. map.put(line[0], new DiNode(line[0], line[1]));
  68. }
  69. }
  70.  
  71. sc.close();
  72. sc = new Scanner(new File("dataset1.csv"));
  73.  
  74. while (sc.hasNext()) {
  75. line = sc.next().split(",");
  76. // Error checks for args
  77. node = map.get(line[0]);
  78. if (node != null) {
  79. node.leg_len = line[1];
  80. node.speed = node.calculate_speed();
  81. }
  82. }
  83.  
  84. for (Map.Entry<String, DiNode> node : map) {
  85. if (node.speed != Integer.MIN_VALUE) {
  86. list.add(node);
  87. }
  88. }
  89.  
  90. mergesort(list);
  91.  
  92. for (DiNode node : list) {
  93. System.out.println(node);
  94. }
  95.  
  96. } catch (FileNotFoundException e) {
  97. e.printStackTrace();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. } finally {
  101. sc.close();
  102. }
  103.  
  104. }
  105.  
  106. }
  107.  
  108. 2nd Q:
  109. host range: web0001-1000.facebook.com
  110. process name: app
  111. optimal number of instances: 4
  112.  
  113. Assume authentication is not an issue.
  114. Give me a list of all the hosts running the optimal number of instances of the given process.
  115.  
  116. def int_to_host():
  117. # ...Input 1, output web0001.facebook.com
  118.  
  119. list = [x for x in range(1,1000)]
  120. host_list = map(int_to_host, list)
  121. output = []
  122.  
  123. for host in host_list:
  124. if ssh(host, "ps aux | grep app | count -n") == 4:
  125. output.append(host)
  126.  
  127. print(output)
Advertisement
Add Comment
Please, Sign In to add comment