fork download
  1. //obstacle mock try this on this online compiler https://w...content-available-to-author-only...b.com/online_java_compiler#
  2. import java.util.*;
  3.  
  4. /**
  5.  * =====================================================================
  6.  * OBSTACLE COURSE — Karat Interview
  7.  * =====================================================================
  8.  *
  9.  * We are writing software to collect and manage data on how fast racers
  10.  * complete obstacle courses. An obstacle course is a series of physical
  11.  * challenges that a racer must go through.
  12.  *
  13.  * A "run" is one attempt at an obstacle course.
  14.  * A "run collection" is a group of runs on a particular course.
  15.  * Each obstacle has a time recorded for it.
  16.  * A run may be incomplete (racer didn't finish all obstacles).
  17.  *
  18.  * Example data:
  19.  * Obstacles: O1 O2 O3 O4
  20.  * Run 1: 3 4 5 6
  21.  * Run 2: 4 4 4 5
  22.  * Run 3: 4 5 4 6
  23.  * Run 4: 5 5 3 (incomplete)
  24.  *
  25.  * TASK 1: The tests are failing. Read the code, find the bug, fix it.
  26.  *
  27.  * TASK 2: Implement bestOfBests().
  28.  * This is a measure of how fast a run could be if everything went
  29.  * perfectly. It is determined by taking the fastest time for each
  30.  * obstacle across all runs (even incomplete ones) and summing them.
  31.  * In the example above: 3, 4, 3, 5 → best of bests = 15 seconds.
  32.  *
  33.  * TASK 3: Implement chanceOfPersonalBest(Run inProgressRun).
  34.  * Given an in-progress run, simulate 10,000 trials. For each
  35.  * remaining obstacle, randomly pick a time from that obstacle's
  36.  * historical times across all existing runs. Return the fraction
  37.  * of trials where the simulated total <= personalBest().
  38.  */
  39.  
  40. class Course {
  41. public String title;
  42. public int obstacleCount;
  43.  
  44. public Course(String title, int obstacleCount) {
  45. this.title = title;
  46. this.obstacleCount = obstacleCount;
  47. }
  48.  
  49. @Override
  50. public boolean equals(Object o) {
  51. if (!(o instanceof Course)) return false;
  52. Course c = (Course) o;
  53. return title.equals(c.title) && obstacleCount == c.obstacleCount;
  54. }
  55. }
  56.  
  57. class Run {
  58. public Course course;
  59. public boolean complete;
  60. public List<Integer> obstacleTimes;
  61.  
  62. public Run(Course course) {
  63. this.course = course;
  64. this.complete = false;
  65. this.obstacleTimes = new ArrayList<>();
  66. }
  67.  
  68. public void addObstacleTime(int time) {
  69. if (complete) throw new IllegalStateException("Cannot add obstacle to complete run");
  70. obstacleTimes.add(time);
  71. if (obstacleTimes.size() == course.obstacleCount) {
  72. complete = true;
  73. }
  74. }
  75.  
  76. public int getRunTime() {
  77. int total = 0;
  78. for (int t : obstacleTimes) total += t;
  79. return total;
  80. }
  81. }
  82.  
  83. class RunCollection {
  84. public List<Run> runs;
  85. public Course course;
  86.  
  87. public RunCollection(Course course) {
  88. this.runs = new ArrayList<>();
  89. this.course = course;
  90. }
  91.  
  92. public int getNumRuns() {
  93. return runs.size();
  94. }
  95.  
  96. public void addRun(Run run) {
  97. if (!run.course.equals(course)) {
  98. throw new IllegalArgumentException("Run's course doesn't match collection's course");
  99. }
  100. runs.add(run);
  101. }
  102.  
  103. // TASK 1: This method has a bug. Find and fix it.
  104. // Returns the fastest complete run time (personal best).
  105. public int personalBest() {
  106. return runs.stream().mapToInt(r -> r.getRunTime()).min().orElse(Integer.MAX_VALUE);
  107. }
  108.  
  109. /**
  110.   * TASK 2: Best of bests.
  111.   * Sum of the fastest time per obstacle across ALL runs (including incomplete).
  112.   */
  113. public int bestOfBests() {
  114. // TODO: implement
  115. return 0;
  116. }
  117.  
  118. /**
  119.   * TASK 3: Chance of personal best.
  120.   * Simulates 10,000 trials completing inProgressRun with random historical times.
  121.   */
  122. public double chanceOfPersonalBest(Run inProgressRun) {
  123. // TODO: implement
  124. return 0.0;
  125. }
  126. }
  127.  
  128. public class Main {
  129.  
  130. private static int passed = 0, failed = 0;
  131.  
  132. public static void main(String[] args) {
  133. System.out.println("\n=== OBSTACLE COURSE — KARAT PRACTICE ===\n");
  134.  
  135. run("TASK 0 — Run basics", Main::testRun);
  136. run("TASK 1 — Personal best", Main::testRunCollection);
  137. run("TASK 2 — Best of bests", Main::testBestOfBests);
  138. run("TASK 3 — Chance of personal best", Main::testChanceOfPersonalBest);
  139.  
  140. System.out.println("\n--------------------------------------------------");
  141. System.out.println("Results: " + passed + " passed, " + failed +
  142. " failed out of " + (passed + failed));
  143. }
  144.  
  145. private static RunCollection makeRunCollection(Course course, int[][] data) {
  146. RunCollection rc = new RunCollection(course);
  147. for (int[] times : data) {
  148. Run run = new Run(course);
  149. for (int t : times) run.addObstacleTime(t);
  150. rc.addRun(run);
  151. }
  152. return rc;
  153. }
  154.  
  155. public static void testRun() {
  156. Course c = new Course("Test", 2);
  157. Run r = new Run(c);
  158. r.addObstacleTime(3);
  159. assert !r.complete : "Should not be complete yet";
  160. r.addObstacleTime(5);
  161. assert r.complete : "Should be complete now";
  162. assert r.getRunTime() == 8 : "Run time should be 8";
  163. try {
  164. r.addObstacleTime(4);
  165. throw new AssertionError("Should have thrown exception");
  166. } catch (IllegalStateException e) {
  167. // expected
  168. }
  169. }
  170.  
  171. public static void testRunCollection() {
  172. Course c = new Course("Test", 4);
  173. int[][] data = {{3,4,5,6}, {4,4,4,5}, {4,5,4,6}, {5,5,3}};
  174. RunCollection rc = makeRunCollection(c, data);
  175.  
  176. assert rc.getNumRuns() == 4 : "Should have 4 runs";
  177. assert rc.personalBest() == 17 :
  178. "Personal best should be 17, was " + rc.personalBest();
  179. }
  180.  
  181. public static void testBestOfBests() {
  182. Course c = new Course("Test", 4);
  183. int[][] data = {{3,4,5,6}, {4,4,4,5}, {4,5,4,6}, {5,5,3}};
  184. RunCollection rc = makeRunCollection(c, data);
  185.  
  186. int bob = rc.bestOfBests();
  187. assert bob != 0 : "bestOfBests() not implemented (returned 0)";
  188. assert bob == 15 :
  189. "Best of bests should be 15, was " + bob;
  190. }
  191. public static void testChanceOfPersonalBest() {
  192. Course c1 = new Course("Test", 3);
  193. int[][] data1 = {{3,3,2}, {3,3,3}};
  194. RunCollection rc1 = makeRunCollection(c1, data1);
  195. Run test1 = new Run(c1);
  196. test1.addObstacleTime(3);
  197. test1.addObstacleTime(3);
  198. double chance1 = rc1.chanceOfPersonalBest(test1);
  199. assert chance1 != 0.0 : "chanceOfPersonalBest() not implemented (returned 0.0)";
  200. assert chance1 >= 0.48 && chance1 <= 0.52 :
  201. "Chance should be ~0.50, was " + chance1;
  202.  
  203. Course c2 = new Course("Test", 4);
  204. int[][] data2 = {{3,3,2,3}, {3,3,3,2}, {5,5,2}};
  205. RunCollection rc2 = makeRunCollection(c2, data2);
  206. Run test2 = new Run(c2);
  207. test2.addObstacleTime(3);
  208. test2.addObstacleTime(3);
  209. double chance2 = rc2.chanceOfPersonalBest(test2);
  210. assert chance2 != 0.0 : "chanceOfPersonalBest() not implemented (returned 0.0)";
  211. assert chance2 >= 0.813 && chance2 <= 0.853 :
  212. "Chance should be ~0.833, was " + chance2;
  213. }
  214.  
  215. // ================================================================
  216. // HELPERS
  217. // ================================================================
  218.  
  219. private static void run(String name, Runnable test) {
  220. try {
  221. test.run();
  222. passed++;
  223. System.out.println(" PASS: " + name);
  224. } catch (Exception | AssertionError e) {
  225. failed++;
  226. System.out.println(" FAIL: " + name + " -> " + e.getMessage());
  227. }
  228. }
  229.  
  230. }
  231.  
Success #stdin #stdout 0.17s 60048KB
stdin
Standard input is empty
stdout
=== OBSTACLE COURSE — KARAT PRACTICE ===

  PASS: TASK 0 — Run basics
  PASS: TASK 1 — Personal best
  PASS: TASK 2 — Best of bests
  PASS: TASK 3 — Chance of personal best

--------------------------------------------------
Results: 4 passed, 0 failed out of 4