Cisco Code pad Interview question.

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2021-06-24Added By Anonymous
Source: Link
Company: Cisco
Role: unknown
Interview Stage: unknown
Year: unknown
Location: unknown

Original Content:
import java.util.*; /** * * DESCRIPTION * ---------------------------- * In our platform, we monitor the paths that network packets follow across the internet. Each * node that is traversed is called a Network Hop. * * Implement a component that identifies pairs of hops were seen in our data pipeline. * * * REQUIREMENTS * ---------------------------- * - Implement a class that counts pairs in a continuous stream of integers. * - For two equal integers to be a pair, they must be separated at most a neighboring distance. * - The stream of integers is unbounded. It can be large. * - A number can only be a part of 2 pairs. One to the left and one to the right. * * NOTES * ---------------------------- * - The solution should be efficient and work correctly * - Disregard concurrency issues * * * EXAMPLES // Let's assume distance is 2 for all examples below * ---------------------------- * StreamPairCounter pairCounter = new StreamPairCounter(2); * pairCounter.addHop(1); * pairCounter.addHop(2); * pairCounter.addHop(1); * * [ 1 2 1 ] | pairCounter.getPairCountFor(1) = 1 * 0 1 2 | pairCounter.getPairCountFor(2) = 0 * * StreamPairCounter pairCounter = new StreamPairCounter(2); * pairCounter.addHop(1); * pairCounter.addHop(2); * pairCounter.addHop(3); * pairCounter.addHop(1); * * [ 1 2 3 1 ] | pairCounter.getPairCountFor(1) = 0 * 0 1 2 3 | pairCounter.getPairCountFor(2) = 0 * | pairCounter.getPairCountFor(3) = 0 * * StreamPairCounter pairCounter = new StreamPairCounter(2); * pairCounter.addHop(1); * pairCounter.addHop(1); * pairCounter.addHop(1); * * // Remember we are interested in pairs with the minimum separation * [ 1 1 1 ] | pairCounter.getPairCountFor(1) = 2 * | pairCounter.getPairCountFor(2) = 0 * | pairCounter.getPairCountFor(9999) = 0 * * * */ /** */ class StreamPairCounter { private final int distance; List<Integer> listOfHops = new ArrayList<>(); /** * distance: neighboring distance (In the example above: 2) */ public StreamPairCounter(int distance) { this.distance = distance; } /** * This method will be called by the client to add a new hop */ public void addHop(int number) { listOfHops.add(number); } /** * Get the count of pairs for a given number */ // My input - 1 2 2 2 3 1 public int getPairCountFor(int number) {//2 boolean booleanIsHopMacthed = false; int counter = 0; for(int i = 0;i < listOfHops.size(); i++){ if (listOfHops.get(i) == number){ //booleanIsHopMacthed = true; // if(booleanIsHopMacthed) counter++; } } return counter; } }
Comments