Panda Guru LogoPanda
Guru

Arista Networks internship on-campus OA round 1 2024 september

Round 1

Questions:

  1. (Easy) Given an array of words, for each word return the minimum number of swaps needed to make adjacent letters different.

    • Example: If input is ['ab', 'aaab', 'abbaaab'], the output will be [0, 1, 2] because:
      • 'ab' already has adjacent letters different,
      • 'aaab' needs only 1 swap for the middle 'a',
      • 'abbaaab' needs a total of 2 swaps (1 for the middle 'a' and 1 for the adjacent 'b's).
    • Solution idea: Use one loop to iterate over each letter, and if the letter is the same as the previous letter, increment a counter variable c by 1. If the letter is different from the previous letter, increment variable ans by c/2 and reset c=1 (because the initial count of the letter should be 1, not 0). Return ans.
  2. (Easy-Medium) Given two integer arrays, find how many swaps are needed to make them equal. Return -1 if not possible. Rearranging within the same array is not counted as a swap.

  3. (Medium) Given a tree represented by two lists (one having vertices from and the other having vertices to with corresponding indices referring to the same vertex), then given three nodes, x, y, z, find the total count of those nodes whose distance from these three nodes forms a Pythagorean triplet.

Candidate's Approach
  1. For the first question, the candidate proposed iterating through each letter of the words and maintaining a count of consecutive identical letters. The approach involves checking if the current letter is the same as the previous one and adjusting the counters accordingly to calculate the number of swaps needed.

  2. For the second question, the candidate would likely consider the frequency of elements in both arrays and determine if they can be made equal through swaps.

  3. For the third question, the candidate would need to implement a method to traverse the tree and check the distances between the specified nodes to identify valid Pythagorean triplets.

Interviewer's Feedback

No feedback provided.