 Meta

## Interview Question

Network Engineer Interview

-

# All questions are mentions in the review and practice following coding questions for sure on leetcode.

1

3. convert the sentence to the modified pig-latin/goat-latin language. Complexity Time: O(n) + O(5) + O(k(k+1)/2) Considering total n characters in sentence. m characters/word O(n): Reading compelete sentence O(2): For vowel. Append ma O(3): For consonant. Append ma and 1st character 1+2+3+... k words = k(k+1)/2 Space: O(n) + 2O(m) + O(5) O(5): unordered_set of vowels 2O(m): Storing 2 word O(n): Storing complete sentence Code #include #include #include void AppendA(std::string& strWord, int& count){ int i = 0; strWord += "ma"; while(i++ us = {'a','e','i','o','u'}; //1st Letter is Vowel if (us.find(std::tolower(strParsed.at(0))) != us.end()) { strWord = strParsed; AppendA(strWord, ++count); } else //consonant { strWord = strParsed.substr(1,strParsed.size()); strWord += strParsed; AppendA(strWord, ++count); } } int main(){ std::string strInput = "Adam wants to go to the university"; std::string strOutput, strWord, strParsed; int count = 0; for (auto& i:strInput){ if (i == ' '){ Process(strParsed, strWord, count); strOutput += strWord + ' '; strWord = ""; strParsed = ""; } else //Store word in strParsed { strParsed += i; } } Process(strParsed, strWord, count); strOutput += strWord ; std::cout<<"Converted string="<

Amit on

0

1. You receive a vector/ array and 2 numbers, eg 2 and 5. Between those 2 positions in the vector (2 and 5) you have to reverse the order of the elements. Code #include #include #include using vec = std::vector; using itr = std::vector::iterator; int main(){ vec v = {1,2,9,8,4,5,23,5,252,445,677,5}; //Find position of 2 itr it1 = std::find(v.begin(), v.end(), 2); //O(n) //Find position of 5 itr it2 = std::find(v.begin(), v.end(), 5); //O(n) std::reverse(it1+1, it2); //O(n) for (auto i:v) std::cout<

Amit on

0

2. Count words and output top n Complexity n: number of words Space map=O(n), priority_queue = O(k). Since we only create queue of size=k, output vector = O(k) Time Creation of map=O(n), Creation of priority Queue=O(n), Creation of output vector=O(k) Code #include #include #include #include using VS = std::vector; using mypair = std::pair; class Solution { public: VS topKFrequent(VS& vsWords, int k) { VS vsOut; std::priority_queue pq; std::unordered_map um; // for(const auto i : vsWords) //1 um[i]++; //um[key]=value; //um[i] = um[i]+1 for(const auto i:um){ //2 pq.push(make_pair(-i.second,i.first)); if (pq.size() > k) pq.pop(); } while(not pq.empty()){ //3 vsOut.push_back(pq.top().second); pq.pop(); } for(int i = 0; i < vsOut.size()/2; i++) //4 std::swap(vsOut[i], vsOut[vsOut.size()-i - 1]); return vsOut; } };

Amit on

0

6. Write a program to read in the data files from disk, it must then print the names of only the bipedal dinosaurs from fastest to slowest. Logic 1. Read dataset2.csv and store in unordered_map , so that we can search name in O(1) time. 2. Read Name,leg_length from dataset1.csv. Search strideLength for Name from unordered_map. Calculate speed using above formula. Store pair in priority_queue(maxHeap). 3. Print second,first from priority_queue and keep on deleting entries. Complexity Space = O(m)+O(n) dataset2.csv=length=n, dataset2.csv=length=m Creation of unordered_map = O(m) //only name, strideLen Creation of maxHeap = O(n) //only Speed, Name Time = O(m)+O(n) Unordered_map creation=O(n), Retrieve key=O(1) Prirotiy Queue creation=O(m), Retrieve = O(1) Code #include #include #include #include #include #include #define r 4 #define r2 r*r #define g 9.8/r2 double CalculateSpeed (double dStrideLen, double dLegLen) { return ((dStrideLen/dLegLen - 1) * sqrt(dLegLen * g)); } int main() { std::string strLine; //1 Line from file std::string strWord; //Word from parsed line std::string strName; //Name from parsed line double dLegLength = 0.0, dStrideLen = 0.0; int i=0; std::unordered_map mapDataSet2; std::map mapOutput; std::priority_queue> maxHeap; std::ifstream ifDataset2 ("dataset2.csv"); //1. Open dataset2.csv std::getline (ifDataset2, strLine); //Ignore 1st line of dataset2.csv //Read dataset2.csv and store in map while (ifDataset2.eof() == 0) { //Read file dataset2.csv std::getline (ifDataset2, strLine); std::stringstream oSSDataSet2(strLine); while (std::getline(oSSDataSet2, strWord, ',')) { if (i==0){ strName = strWord; } if (i==1){ std::istringstream(strWord) >> dStrideLen; mapDataSet2[strName] = dStrideLen; } ++i; } i=0; } std::cout> dLegLength; auto it = mapDataSet2.find(strName); if(it != mapDataSet2.end()){ dSpeed = CalculateSpeed(it->second, dLegLength); // std::cout p = maxHeap.top(); std::cout<

Amit on

1

7. Given a string, determine if it is a palindrome. - Complexity: Time: O(n) - Code #include #include int main(){ std::string strInput = "abcba"; int i=0, j=strInput.size()-1; while(i

Amit on

0

9. generate a minesweeper grid (2x3) GITHUB/Code-examples/blob/master/ds_questions/questions/vectors_arrays/2d-grid/Minesweeper%20_Game.md

Amit on

0

13. Write a function to sort a list of integers that looks like this [5,2,0,3,0,1,6,0] to [1,2,3,5,6,0,0,0] in the most efficient way. using vec = std::vector; class Solution { public: void moveZeroes(vec& nums) { int i=0, j=nums.size()-1; while (i0) std::sort(nums.begin(), nums.begin()+i); } };

Anonymous on

0

15. Given a list of integers, output all subsets of size three, which sum to zero. using vec =std::vector; using vec2d =std::vector; std::vector threeSum(vec& nums) { vec2d out; if(nums.size() -(*a)) --c; else if (sum < -(*a)) ++b; else{ //sum == -(*a) out.push_back({(*a), *b, *c}); ++b; //if prev_element==present_element. Skip present element while(b

Amit on

0

16. Extract and modify emails/URLs from HTML files. GITHUB.COM/amitkumar50/Code-examples/blob/master/ds_questions/questions/FileHandling/Read_Modify_emailsURLs_from_HTML_files.md

Amit on

0

17. Find a fast way to extract patterns from a 2d matrix. GITHUB/amitkumar50/Code-examples/blob/master/ds_questions/questions/strings/2D-Grid/SearchWord_in_2DGridOfChars-8Directions.md

Amit on

0

19. Reverse a part of a vector, anonymize email addresses GITHUB.COM/amitkumar50/Code-examples/blob/master/ds_questions/questions/strings/Reverse/Reverse_Part_of_vector_anonmize_Email.md

Amit on

0

20. In and array [A, B ,C, D, E , F, G, H] , User input 2 values, e.g. D, F - It's necessary to print [A, B, C, F, E, D, G, H] Character Array: GITHUB.COM/amitkumar50/Code-examples/blob/master/ds_questions/questions/strings/Arrange_Shuffle_Organize/Swap_2_entries_in_Sorted_Array.md Integer Array: GITHUB.COM/amitkumar50/Code-examples/blob/master/ds_questions/questions/vectors_arrays/Order_Arrangment/Swap_2_entries_in_Sorted_Array.md

Amit on

0

4. split the array in such a way that their sum is the same Logic: 2 pointer approach Code bool fun(std::vector& a) { int i=0, j = a.size() - 1; int leftSum = a[i], rightSum = a[j]; while(i

Amit on

2

1. You receive a vector/ array and 2 numbers, eg 2 and 5. Between those 2 positions in the vector (2 and 5) you have to reverse the order of the elements. 2. Count words and output top n 3. convert the sentence to the modified pig-latin/goat-latin language. 4. split the array in such a way that their sum is the same. 5. read the innate file and parse the strings to count how many times an email address is found. 6. Write a program to read in the data files from disk, it must then print the names of only the bipedal dinosaurs from fastest to slowest. 7. Given a string, determine if it is a palindrome. 8. Given a file with multiple columns, print the first and the third column out and find a new value based on the values of those two columns. 9. generate a minesweeper grid (2x3) 10. Battleships in a Board 11. Bulb Switcher 12. Binary tree traversal 13. Write a function to sort a list of integers that looks like this [5,2,0,3,0,1,6,0] to [1,2,3,5,6,0,0,0] in the most efficient way. 14. Given a list of ([4, 5, 6, 1, 2, 3]), search for a given integer in the list. [4, 5, 1, 2, 3] 15. Given a list of integers, output all subsets of size three, which sum to zero. 16. Extract and modify emails/URLs from HTML files. 17. Find a fast way to extract patterns from a 2d matrix. 18. Take a paragraph as Input and output the top three most repeated words 19. Reverse a part of a vector, anonymize email addresses 20. In and array [A, B ,C, D, E , F, G, H] , User input 2 values, e.g. D, F - It's necessary to print [A, B, C, F, E, D, G, H]

Anonymous on