NewtGlobal Interview Question

DCS Software Engineering Tech Screen Questions 1. ConvertToNumber – Find Flaws and Limitations Identify the flaws / limitations in the following C# ConvertToNumber method: public static bool ConvertToNumber(string str) { bool canConvert = false; try { int n = Int16.Parse(str); if (n != 0) { canConvert = true; } } catch (Exception ex) { } bool retval = false; if (canConvert == true) { retval = true; } return retval; } 2. Increment & Decrement – Find The Bugs! The following C# code is intended to increment / decrement A and B until A is equal to X and B is equal to Y. Find the bugs and suggest fixes. public static void MakeTheNumbersMatch(int a, int b, int x, int y) { while (a != x && b != y) { if (a > x) { a++; } else { a--; } if (b > y) { b++; } else { b--; } } } 3. Mystery Method Describe what the Mystery method does (C#) and discuss any potential bugs and possible fixes. public class P { public string Name; public P[] Acquaintances; public P(string name, P[] acquaintances) { if (String.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Name cannot be null or white space.", "name"); } this.Name = name; this.Acquaintances = acquaintances; } public bool Mystery(string name) { if (String.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Name cannot be null or white space.", "name"); } Stack<Person> myStack = new Stack<P>(); foreach (P acquaintance in this.Acquaintances) { myStack.Push(acquaintance); } do { var person = myStack.Pop(); if (person.Name.Equals(name)) { return true; } foreach (P acquaintance in person.Acquaintances) { myStack.Push(acquaintance); } } while (myStack.Count>= 0); return false; } } 4. Sort Products by Priority Products are identified by alphanumeric codes. Each code is stored as a string. We have three types of products: high priority, medium priority, and low priority. Given an array of product codes, sort the array so that the highest priority products come at the beginning of the array, the medium priority products come in the middle, and the low priority products come at the end. Within a priority group, order does not matter. You are given a priority function which, given a product code, returns 1 for high, 2 for medium and 3 for low. This array may contain a large number of product codes, so do your best to minimize additional storage. You are given this function for usage: private int GetPriority(string productCode). You don’t need to implement this function. Please Implement the following function. You may use C# or Java: public void OrderProductsByPriority(string[] productCodes) 5 Generate the Lowest Number You are tasked with implementing a method that returns the lowest possible number that could be generated after removing n characters from a string of digits. The method signature should look like: public static string GenerateLowestNumber(string number, int n) Where the number parameter is a string that contains a number (e.g. “4205123”), and the n parameter represents the number of characters to remove from the string. The goal of this method is to return the lowest number that can be generated by removing n characters from the number provided while keeping the positions of the remaining characters relative to each other the same (i.e. the method should remove n characters from the string, but it cannot re-order the characters). For example, if number is “4205123” and n is 4, the lowest possible number that can be generated after removing any 4 characters is “012”. If number is “216504” and n is 3, the lowest possible number that can be generated after removing 3 characters is “104”. Write your response using either C# or Java.

Interview Answer

Anonymous

Apr 18, 2020

#4 public void sortArray(String productCodes[]){ System.out.println(Arrays.toString(productCodes)); int mid=0; int low=0; int high = productCodes.length-1; while(mid<=high){ int priorityCode = getPriority(productCodes[mid]); String tmp = null; switch(priorityCode){ case 1: tmp = productCodes[mid]; productCodes[mid] = productCodes[low]; productCodes[low]=tmp; mid++; low++; break; case 2: mid++; break; case 3: tmp = productCodes[mid]; productCodes[mid] = productCodes[high]; productCodes[high]=tmp; high--; break; } } System.out.println("After Sorting: "+Arrays.toString(productCodes)); }

2