Questions d'entretiens - Senior software developer
Questions d'entretien de senior software developer partagées par les candidats
Le top des questions d'entretien
Write a function that divides one number by another, without using the division operator, and make it better than O(n). 7 réponsesThis can be done in a recursive function, the following code is in Python. # get result of a/b without using a "divide" operator def div(a,b): if a < b: return 0 else: return div(a-b, b)+1 This is how human being do the division naturally, however, the running time of this is O(n/m), where n is the size of a, and m is the size of b, which means, O(n/m) is guaranteed to be less than O(n), when m is larger than 1. -Maxim The answer above is still O(n). We can use binary search and find the answer in the interval [1,a] and use multiplication operator. Totally agree with Vasil. Other option: Long Division Algorithm. O(log n) anyway. Afficher plus de réponses why not just a * b^(-1) :-) // Write a function that divides one number by another, without using the division operator, // Assume that x%y = 0 // O(log n) (function() { 'use strict'; var divide = function(x, y) { var xLength = (x + '').length; var i = 0; var result = 0; var xAry = ('' + x).split(); var xStart = ''; for (i = 1; i = y) { xStart -= y; result = parseInt(result, 10) + 1; } } } return result; }; console.log(divide(1000, 4)); })(); Use logarithms? O(1) log(x/y) = log(x) - log(y) = log(answer) answer = 10 ^ log(answer) Convert the number to divide into the base of the number that you are dividing with and then shift the 'bits' to the right by 1 then convert back to decimal |
Given an array of int[] like 1,2,3 Find the next largest integer than can be made with these digits (e.g.: 2,1,3) 5 réponsesWrote code in C# to solve the problem in a generic case. Think about this situation: 5,9,8,7,6,4,3,1,2 -> 5,9,8,7,6,4,3,2,1 followed by 5,9,8,7,6,4,3,2,1 -> 6,1,2,3,4,5,7,8,9. // Typescript implementation // Assumption 1: all numbers in the input array are single digits, i.e. between 0 and 9 // Assumption 2: if a larger number cannot be found then return the current number as is const getNextLargestNumber = (inputs: number[]): number => { if (inputs) { // Do not mutate the initial inputs let copiedInputs: number[] = inputs.slice(); // Loop through the digits in reverse order // Exclude the first digit for (let i = copiedInputs.length - 1; i >= 1; i--) { // Loop through the previous digits for (let j = i - 1; j >= 0; j--) { const swapFrom: number = copiedInputs[i]; const swapTo: number = copiedInputs[j]; // Swap if a smaller digit is found before if (swapTo x - y)]; return convertArrayToNumber(copiedInputs); } } } } return convertArrayToNumber(inputs); }; const convertArrayToNumber = (inputs: number[]): number => { let num: number = 0; if (inputs) { const inputsLength: number = inputs.length; for (let i = 0; i < inputsLength; i++) { // Use index to determine the power of 10 num += inputs[i] * (10 ** (inputsLength - i - 1)); } } return num; }; console.log(getNextLargestNumber([4, 1, 9, 5])); console.log(getNextLargestNumber([4, 5, 1, 9])); console.log(getNextLargestNumber([4, 5, 9, 1])); console.log(getNextLargestNumber([4, 9, 1, 5])); console.log(getNextLargestNumber([4, 9, 5, 1])); console.log(getNextLargestNumber([5, 1, 4, 9])); console.log(getNextLargestNumber([1, 4, 2, 4, 3, 8, 6, 4, 0])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 3, 6, 8])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 3, 8, 6])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 6, 3, 8])); console.log(getNextLargestNumber([5, 9, 8, 7, 6, 4, 3, 1, 2])); console.log(getNextLargestNumber([5, 9, 8, 7, 6, 4, 3, 2, 1])); console.log(getNextLargestNumber([6, 1, 2, 3, 4, 5, 7, 8, 9])); console.log(getNextLargestNumber([6])); console.log(getNextLargestNumber([6, 5])); console.log(getNextLargestNumber([5, 6])); // Wrong copy-paste // Corrected version // Typescript implementation // Assumption 1: all numbers in the input array are single digits, i.e. between 0 and 9 // Assumption 2: if a larger number cannot be found then return the current number as is const getNextLargestNumber = (inputs: number[]): number => { if (inputs) { // Do not mutate the initial inputs let copiedInputs: number[] = inputs.slice(); // Loop through the digits in reverse order // Exclude the first digit for (let i = copiedInputs.length - 1; i >= 1; i--) { // Loop through the previous digits for (let j = i - 1; j >= 0; j--) { const swapFrom: number = copiedInputs[i]; const swapTo: number = copiedInputs[j]; // Swap if a smaller digit is found before if (swapTo x - y)]; return convertArrayToNumber(copiedInputs); } } } } return convertArrayToNumber(inputs); }; const convertArrayToNumber = (inputs: number[]): number => { let num: number = 0; if (inputs) { const inputsLength: number = inputs.length; for (let i = 0; i < inputsLength; i++) { // Use index to determine the power of 10 num += inputs[i] * (10 ** (inputsLength - i - 1)); } } return num; }; console.log(getNextLargestNumber([4, 1, 9, 5])); console.log(getNextLargestNumber([4, 5, 1, 9])); console.log(getNextLargestNumber([4, 5, 9, 1])); console.log(getNextLargestNumber([4, 9, 1, 5])); console.log(getNextLargestNumber([4, 9, 5, 1])); console.log(getNextLargestNumber([5, 1, 4, 9])); console.log(getNextLargestNumber([1, 4, 2, 4, 3, 8, 6, 4, 0])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 3, 6, 8])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 3, 8, 6])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 6, 3, 8])); console.log(getNextLargestNumber([5, 9, 8, 7, 6, 4, 3, 1, 2])); console.log(getNextLargestNumber([5, 9, 8, 7, 6, 4, 3, 2, 1])); console.log(getNextLargestNumber([6, 1, 2, 3, 4, 5, 7, 8, 9])); console.log(getNextLargestNumber([6])); console.log(getNextLargestNumber([6, 5])); console.log(getNextLargestNumber([5, 6])); Afficher plus de réponses // Hmm, after posting the comment, some code gets changed (after "Swap if a smaller digit is found before" comment) // Attempt 3 to post // Typescript implementation // Assumption 1: all numbers in the input array are single digits, i.e. between 0 and 9 // Assumption 2: if a larger number cannot be found then return the current number as is const getNextLargestNumber = (inputs: number[]): number => { if (inputs) { // Do not mutate the initial inputs let copiedInputs: number[] = inputs.slice(); // Loop through the digits in reverse order // Exclude the first digit for (let i = copiedInputs.length - 1; i >= 1; i--) { // Loop through the previous digits for (let j = i - 1; j >= 0; j--) { const swapFrom: number = copiedInputs[i]; const swapTo: number = copiedInputs[j]; // Swap if a smaller digit is found before if (swapTo x - y) ]; return convertArrayToNumber(copiedInputs); } } } } return convertArrayToNumber(inputs); }; const convertArrayToNumber = (inputs: number[]): number => { let num: number = 0; if (inputs) { const inputsLength: number = inputs.length; for (let i = 0; i < inputsLength; i++) { // Use index to determine the power of 10 num += inputs[i] * (10 ** (inputsLength - i - 1)); } } return num; }; console.log(getNextLargestNumber([4, 1, 9, 5])); console.log(getNextLargestNumber([4, 5, 1, 9])); console.log(getNextLargestNumber([4, 5, 9, 1])); console.log(getNextLargestNumber([4, 9, 1, 5])); console.log(getNextLargestNumber([4, 9, 5, 1])); console.log(getNextLargestNumber([5, 1, 4, 9])); console.log(getNextLargestNumber([1, 4, 2, 4, 3, 8, 6, 4, 0])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 3, 6, 8])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 3, 8, 6])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 6, 3, 8])); console.log(getNextLargestNumber([5, 9, 8, 7, 6, 4, 3, 1, 2])); console.log(getNextLargestNumber([5, 9, 8, 7, 6, 4, 3, 2, 1])); console.log(getNextLargestNumber([6, 1, 2, 3, 4, 5, 7, 8, 9])); console.log(getNextLargestNumber([6])); console.log(getNextLargestNumber([6, 5])); console.log(getNextLargestNumber([5, 6])); // Last attempt // Possibly, angle brackets break the code sample // Typescript implementation // Assumption 1: all numbers in the input array are single digits, i.e. between 0 and 9 // Assumption 2: if a larger number cannot be found then return the current number as is const getNextLargestNumber = (inputs: number[]): number => { if (inputs) { // Do not mutate the initial inputs let copiedInputs: number[] = inputs.slice(); // Loop through the digits in reverse order // Exclude the first digit for (let i = copiedInputs.length - 1; i >= 1; i--) { // Loop through the previous digits for (let j = i - 1; j >= 0; j--) { const swapFrom: number = copiedInputs[i]; const swapTo: number = copiedInputs[j]; // Swap if a smaller digit is found before if (swapTo < swapFrom) { copiedInputs[i] = swapTo; copiedInputs[j] = swapFrom; // Take portion of array up to the swap point // The remaining digits are sorted in ascending order copiedInputs = [...copiedInputs.slice(0, j + 1), ...copiedInputs.slice(j + 1).sort((x, y) => x - y)]; return convertArrayToNumber(copiedInputs); } } } } return convertArrayToNumber(inputs); }; const convertArrayToNumber = (inputs: number[]): number => { let num: number = 0; if (inputs) { const inputsLength: number = inputs.length; for (let i = 0; i < inputsLength; i++) { // Use index to determine the power of 10 num += inputs[i] * (10 ** (inputsLength - i - 1)); } } return num; }; console.log(getNextLargestNumber([4, 1, 9, 5])); console.log(getNextLargestNumber([4, 5, 1, 9])); console.log(getNextLargestNumber([4, 5, 9, 1])); console.log(getNextLargestNumber([4, 9, 1, 5])); console.log(getNextLargestNumber([4, 9, 5, 1])); console.log(getNextLargestNumber([5, 1, 4, 9])); console.log(getNextLargestNumber([1, 4, 2, 4, 3, 8, 6, 4, 0])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 3, 6, 8])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 3, 8, 6])); console.log(getNextLargestNumber([1, 4, 2, 4, 4, 0, 6, 3, 8])); console.log(getNextLargestNumber([5, 9, 8, 7, 6, 4, 3, 1, 2])); console.log(getNextLargestNumber([5, 9, 8, 7, 6, 4, 3, 2, 1])); console.log(getNextLargestNumber([6, 1, 2, 3, 4, 5, 7, 8, 9])); console.log(getNextLargestNumber([6])); console.log(getNextLargestNumber([6, 5])); console.log(getNextLargestNumber([5, 6])); |
find LCA for two nodes of a binary tree. 4 réponsesList 1: tree nodes inorder List 2: tree nodes postorder List 3: all the nodes in between the given 2 nodes List 4: all the nodes after the given 2 nodes the LCA is the common node in List 3 and List 4. I think there's an easier solution: List1: the parents of node1 in order bottom to top (can get them by navigating up tree from node 1). Navigate up tree from node2. First parent of node 2 found in list1 is LCA. Afficher plus de réponses The advantage? Well complexity is log (n) and there is no extra memory required i.e. no list creation needed Un ou plusieurs commentaires ont été supprimés. |
write a c# method to bring pairs of integers that sum up to 10 from an array of integers. 3 réponsesThey said the solution should have to sorted the integer array, put two pointers at the end and front compare the sum of a and b, if it was more than 10 then bring the last pointer forward until end of array ... They best solution was (On), and I gave them( lgn)... The other solution was to build a dictionary with pairs and each time adding new element check to see if it already exited ... #include #include #include static std::map findPairsOfSum(int array[], int sum){ std::map myPair; int len = sizeof(array); if (array == NULL) return myPair; for (int i=0; i pairs; int myArray[] = {0,10,3,7,3,5,7,3}; pairs = findPairsOfSum(myArray,10); // printing map std::map :: iterator it; for (it = pairs.begin(); it != pairs.end(); ++it){ std::coutfirstsecond myPairs(int [] myArr, int sumUp){ Dictionary returnPair = new Dictionary (); int len = myArr.Length; Console.WriteLine("Array Size -> "+len); for (int i=0;i myDictionary = myPairs(myArray,10); foreach (var item in myDictionary) { Console.WriteLine("key {0} and value {0}",item.Key,item.Value); } } } } #include #include #include static std::map findPairsOfSum(int array[], int sum){ std::map myPair; int len = sizeof(array); if (array == NULL) return myPair; for (int i=0; i pairs; int myArray[] = {0,10,3,7,3,5,7,3}; pairs = findPairsOfSum(myArray,10); // printing map std::map :: iterator it; for (it = pairs.begin(); it != pairs.end(); ++it){ std::coutfirstsecond <<'\n'; } return 0; } using System; using System.Collections.Generic; namespace pairsSum { class Program { public static Dictionary myPairs(int [] myArr, int sumUp){ Dictionary returnPair = new Dictionary (); int len = myArr.Length; Console.WriteLine("Array Size -> "+len); for (int i=0;i myDictionary = myPairs(myArray,10); foreach (var item in myDictionary) { Console.WriteLine("key {0} and value {0}",item.Key,item.Value); } } } } |
After asking the details of my current role, he only gave me a simple coding question. Write a function using C++ or Java that is passed an integer and it returns the number of bits set to 1. Is there a way to improve your solution and make it faster and more efficient? 3 réponsesThere are obviously multiple solutions: Solution 1: Set sum = 0 Find the remainder by dividing by 2. Divide by 2 for the next iteration. Solution 2--much better. Set sum = 0 Start loop Set mask = 1 sum += mask & number Loop return sum There is another way, that is explained here: http://en.wikipedia.org/wiki/Hamming_weigh (with just 24 operation and without any cycle you can find the number of bit set to 1) An example in Java with 10000 results in answer 5 int number = 10000; int numberOfBitsSet = 0; for (int i = 1; i <= number;) { int result = (i & number); if (result != 0) { numberOfBitsSet++; } i = i << 1; } System.out.println(numberOfBitsSet); |
A HackerRank challenge with 90 mins to solve 3 réponsesI Solved it, but not the best code I've ever written as I had only 90mins. Through questions like this, interviewers are mostly trying to test your skillset (and its relevance to the role) as robustly as possible, so be prepared for multiple offshoots and followups. It could be a useful exercise to do mocks with friends or colleagues in Hopper to get a real sense of what the interview is actually like. Alternatively Prepfully has a ton of Hopper Senior Software Engineer - Microservices experts who provide mock interviews for a pretty reasonable amount. prepfully.com/practice-interviews any more details on this question? |
I was given a code to review. I have fixed bugs in their code, made it work, and put detailed comments to all modified lines of code plus detailed comments outside of the code describing what was done and why. The answer was that their student could not understand the comments. Hm, I guess the student should be taught better and I didn't apply for a teacher position. They refused to give me detailed answer, but after all I wasn't sure I wanted it. What a company where students decide whether to hire seniors. After some thinking I've concluded that I was simply used as a free independent code reviewer. If you are experienced developer, avoid these guys, they will pretend they are hiring you, but in fact will just use you. If you are freshly graduated, lazy and not knowledgeable, you can go there, they will teach you how to illegally make others do your job for you. 2 réponsesI have fixed bugs in their code, made it work, and put detailed comments to all modified lines of code plus detailed comments outside of the code describing what was done and why. The answer was that their student could not understand the comments. Hm, I guess the student should be taught better and I didn't apply for a teacher position. They refused to give me detailed answer, but after all I wasn't sure I wanted it. What a company where students decide whether to hire seniors. After some thinking I've concluded that I was simply used as a free independent code reviewer. Thank you for the feedback. As the VP of Engineering, I'm always interested in how our job candidates both perceive the overall interview process and the feedback they receive throughout - regardless of the overall outcome. I'm sorry the interview process wasn’t what you expected, but we use the code review exercise as a great tool to understand how you (as a senior developer) would mentor junior employees, what you look for in coding standards and how you think about things like test coverage and code performance. We aren't looking for the "right" answer but instead a conversation and insight into how you develop and what you see as important aspects of quality development. I do want to clarify a few points. First, the code sample is not from our product. It is a made up example to really drive that conversation mentioned above. However, we do appreciate the time you took to do the exercise. Second, while you aren’t looking to be a "teacher", we expect all of our senior talent to be able to mentor and teach. That's not negotiable and we take a lot of pride in our open and sharing culture where many methods are used to share best practices, innovative solutions and raise the bar around code quality and performance (including code reviews). That culture also enables us to hire amazing new grads and know that they will both be supported and quickly grow their own skill sets and abilities. We’ll make sure to clarify that in our future job postings. We're pretty proud of the team we've built at Roadmunk and the culture of code quality, teamwork and mentoring that we're fostering and constantly improving. The interview process is defined to showcase that culture and determine how or if a candidate would fit within it. And for the record, the only lazy thing at Roadmunk is the occasional dog that spends the day with us as we work amazingly hard to deliver our industry-leading roadmapping software. I welcome you to reach out to me directly to discuss further and I wish you luck in your job search! |
What would the count for the list of it. public class Test { string Data; int Id; public Test(int id, string data) { Data = data; Id = id; } } static void Main(string[] args) { List First count would be 2. To make it 1. Add override method as following. public override bool Equals(object obj) { if (obj == null) return false; var d = (Test)obj; return (d.Data == Data && d.Id == Id); } public class Test : IEquatable { string Data; int Id; public Test(int id, string data) { Data = data; Id = id; } public bool Equals(Test other) { return this.Id == other.Id && this.Data == other.Data; } } |
how to merge two sorted linklist? 2 réponsesUse MergeSort. You don't need merge sort here. This is a O(n) task, because the lists are already sorted. In fact, this merge routine is part of merge sort. No sorting takes place here, only merging, and it could be done in linear time. Node* merge(Node* list1, Node* list2) { Node* merged = null; Node** tail = &merged; while (list1 && list2) { if (list1->data data) { *tail = list1; list1 = list1->next; } else { *tail = list2; list2 = list2->next; } tail = &((*tail)->next); } *tail = list1 ? list1 : list2; return merged; } |
What you do if you cannot get something from upper management you need? 2 réponsesvery typical question in many companies this day These should not be issues, upper management should be aware of these needs from the get go. A clear and concise communication plan as well as regular engagement reviews including upper management should always be in place. Unless logic and common sense are not the norm this should not occur. This said each project or initiative should have its sponsorship clearly defined and hopefully proper support already in place |