The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Tag Info

Hot answers tagged interview-questions

8

Inconsistency This is a minor thing, but I tripped over it when skimming your code: throw2 = (throws.Length == 1) ? 0 : throws; throw3 = (throws.Length == 3) ? throws : 0; Why did you invert one of the ternaries? It'd be easier to read if you kept the same structure, e.g. throw2 = (throws.Length >= 2) ? throws : 0; throw3 = (throws.Length >...

5

Private versus public public int frame = 0; // The current frame public int frameScore = 0; // The total of first two throws in a normal frame public int[] throws; // Input public int throw1; // First throw in a frame public int throw2; // Second throw in a frame public int throw3; ...

5

That's a fun problem. Don't use more memory than you have to with open('input.txt') as file: # read the input file into a list of lines file_input = file.read().splitlines() # ... for var in file_input: LonUndOutages.append(var.split("|")) shouldn't be necessary. Rather than calling file.read(), iterate on the file object itself. It will yield ...

4

Usability Your design is pretty string-heavy. If you want to build reusable functions, you should try to avoid this and work with more appropriate types given the context. One such example is your interface design: interface ICalculatable<Parcel> { string Calculator(Parcel p); } What can consumers do with the returned string? This interface can ...

4

You have a more fundamental problem here than performance: your solution isn't actually correct. Consider 4 lego pieces all of size 1. Your solution combines them as $1+1=2$ $2+1=3$ $3+1=4$ for a total cost of $2+3+4 = 9$. However, we can combine more efficiently in the following way $1+1=2$ $1+1=2$ $2+2=4$ for a total cost of $8$. In ...

4

As TorbenPutkonen already said, the good way to improve the performance of your solution is relying on primitive types using Arrays.sort(int[] and rewriting of loop as he suggested. I add my thoughts and code, consider that for your interviewer can be edge cases like o or 1 toy blocks , in this case the shortest time is equal to 0. Now you can use a method ...

4

You could use reverse order and multiply by array index +1 (except last element): 2 + 4 2 + 4 + 5 2 + 4 + 5 + 8 Could be transformed to: element index result 8 * 1 8 5 * 2 10 4 * 3 12 2 * 3 6 36 It allows you to remove reverse counter. Also you can use Array it consume less ...

4

Your inner loop contains an addition, multiplication, subtraction and two assignments. The multiplication is unnecessary as you can do with two additions and two assignments: int totalTimeSpent = 0; int sumOfPair = lego.get(0); for (int i = 1; i < lego.size(); i++) { sumOfPair += lego.get(i); totalTimeSpent += sumOfPair; } Regarding the data ...

3

In your private static int TryDirection(int[][] grid, int x, int y, Queue<int[]> Q, int countFreshOranges) method I would invert the logic: If the given coordinate is valid and the field contains a fresh orange, then do something: private static int TryDirection(int[][] grid, int x, int y, Queue<int[]> Q, int countFreshOranges) { if (x &...

3

Memory Usage When reading from the file, you can assign create the list there instead of reading lines, splitting them, and appending to the list later in the program. It can be as easy as this with open('input.txt') as file: outages = [line.split("|") for line in file] Variable Naming PEP-8 guidelines say that variables should be in snake_case. The ...

2

public enum SizeSmall { length = 200, breadth = 300, height = 150, } public enum SizeMedium { length = 300, breadth = 400, height = 200, } public enum SizeLarge { length = 400, breadth = 600, height = 250 } Your use of the enum type is a little unusual. ...

2

White Space This is hard on my aging eyes: static String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-", ".--","-..-","-.--","--.."}; This is much easier to read: static String[] morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "...

2

The main thing which can be recognized as a Junior Developer code is a poor possibility of code support in the future. To archive such a point you need to use some common architectural patterns. First of all the code should be tested and error handled. Also, you were asked to provide SOLID code which again means a bunch of patterns (IoC as DI is a must ...

2

I also think you are using older styles. For example: private bool _loaded; public bool Loaded { get => _loaded; set => _loaded = value; } Is awkward and really should have a better name: public bool IsLoaded {get; set;} Looks like they would appreciate some OOP. I would think there would be a class for airports. Just Googling on 3 letter ...

2

The first step in your method is to determine the longest common prefix. Here it would be sufficient to determine the length of the longest common prefix. Building the prefix string itself is not necessary at this point. The next step is to decrease that length until it divides both string lengths. An explaining comment would be helpful here. We still don't ...

1

To the many useful points already made I have only a small contribution to add: public class Test { public static void Main() { BowlingGame newGame = new BowlingGame(); // Test first game - SUCCESS! (190) //newGame.RecordFrame(4, 6); //newGame.RecordFrame(3, 6); //newGame....

1

This mostly a response to your comment, but I feel it really shouldn't be another comment. I guess I should have also done the following (?): 1) returned a helpful error message if a non-string argument was given to get_anagrams() and created a unit test for this 2) Created unit test for get_anagrams() with empty string argument (although I think this is ...

1

For an interview, I would test everything very thoroughly. The public interface consists of Anagrams(), Anagrams.initialise(), and Anagrams.get_anagram(). The public interface also includes the fact that initialise() needs to be called before get_anagram() and the format of text file. So test everything: For Anagram(filename): (some of these errors don'...

1

It's hard to give you a full review, because the code complexity is too high.. Little Typo Respondent instead of Respondant. You did not Implement the Task If the respondent cannot handle the call, they must escalate it to a manager. The Respondent handles his call: if (call.level == 0) { System.out.println(name + " handling call: " + call); ...

Only top voted, non community-wiki answers of a minimum length are eligible