# Tag Info

## Hot answers tagged interview-questions

16

Don't use sum as a variable. Especially because you want to use it with your approach! sum = 0 for i in s: sum += values[i] could become: number = sum(values[i] for i in s) s[i] is a string. s[i] + s[i+1] is a string. There is no need to use str() to convert what is already a string to a string. But s[i:i+2] is easier still. With 3999 as the ...

10

Try to use more Python idioms; in particular, make your code more declarative rather than imperative. You can rewrite everything after your dictionary definitions with return sum(values[i] for i in s)+sum(checks.get(s[i:i+2], 0) for i in range(len(s)-1)) or, which is more PEP-8 friendly, result = sum(values[i] for i in s) result += sum(checks.get(s[i : i +...

8

Inconsistency This is a minor thing, but I tripped over it when skimming your code: throw2 = (throws.Length == 1) ? 0 : throws[1]; throw3 = (throws.Length == 3) ? throws[2] : 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[1] : 0; throw3 = (throws.Length >...

7

The intention of classes in object oriented programming is not to replace if sentences, but to model code that differs in behavior. Your 4 classes can be merged into 2, since the first 3 of them use structurally identical code. The code could then be: var rules = new List<IRule> { new ModWordRule(3, "Fizz"), new ModWordRule(5, "Buzz"), new ...

7

I am %100 sure that this is the correct answer, I'm not so sure... I dumped your code into an online compiler*. I made very minor adjustments, moving target to a parameter and adding semicolons which aren't a big deal since you probably didn't have a compiler when writing it. I did not modify your code beyond that and treated it like a black-box. import ...

7

What I am entirely missing from your implementation is any correctness checks on the input of roman numerals. This makes it possible to reverse Roman literals, for instance, without any indication that they are invalid. Even if there are bad characters or lowercase characters the program will just crash. That's not any way to behave. One little trick I used ...

6

Review Well done, there isn't much I would change in this implementation. I would grant you the humble badge for putting yourself as LowLevel employee :) The lazy and null pattern are well implemented (except a small issue that could possibly introduce null -> see minor issues). The chain of responsibility looks to be by the book. You've even used a ...

6

why someone just write Overly Complex 2 times I don't know what format you originally received this information in, but it looks like there are 4 "tags" applied to your application: Overly Complex Solution Strange Coding Conventions Poorly Structured Hard to Understand With someone having modified the first one to include some extra information: ...

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 ...

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

First impression Wow, there's a lot of code for a task that seems quite simple at the first glance. That's not necessarily a bad thing as I do not know what the interviewers are looking for. Main class You have seven different classes but still there is logic in the main method. I would have wanted to see the main method just contain a single setup call ...

5

Before handing the company any code you wrote, you should ask for clarification of the task. Write a JAVA program that will: The correct spelling is Java, not JAVA. Generate a file with random numeric (range from 1 to 2^64 − 1 integers) data. The grammar is slightly wrong here. They should have written "with random integers in the range from 1 ...

5

A way to optimize it would be to return as soon as you have found a duplicate, unless the question specifically asks you to list all duplicates. You could refactor it to something like that: public static boolean checkUnique(String s) { int len = s.length(); for (int i = 0; i < len; i++) { int c = s.codePointAt(i); int next = s....

4

They both look like they are $O(n)$. A quick check with timeit show they both take essentially the same amount of time, and the time grows linearly with the length of the input string. It would be better to iterate over the characters in the string rather use an explicit index over the length of the string (while curr < len(s) is often a smell). def ...

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 ...

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

In addition to @TorbenPutkonen's review here are my comments: In general, the code seems well structured, readable and understandable. Tests While there is a complex logic in your code, the tests are not implemented. Without tests most interviewers would consider the task not completed (as long as it is not an Intern or a Junior position). Logs Although ...

4

First impression is the code is well-documented and is easy to read, especially given the context of it being an interview assignment. But there are definitely places where it can be improved, so let's start with the low-hanging fruit: execution time performance and memory consumption. requests.Session All API calls are to the same host, so we can take ...

4

I have little to add to what Pod said. There is nothing wrong with compact code. Your code could be very efficient, and there is clearly a lot of effort in it, but I agree readability could be improved. Indeed the small details count, even whitespace is important in code. The important takeaways: Good code should have a natural flow and should be visually ...

4

All paths do not return def canJumpPos(pos): if pos >= len(nums): return False elif pos in memo: return memo[pos] else: for i in range(nums[pos], 0, -1): if canJumpPos(i + pos): return True memo[pos] = False # Missing Return This ...

4

I think considering approach that you chose, your code is alright. A few points: What I don't like is that you get error when passing 2 empty arrays? Why would you do that? Imagine I am generating arrays of different sizes and using your code to merge and sometimes I would pass 2 empty arrays. Should I really need to handle that as special case in my code? ...

4

Avoid using in header files It looks like the code you have written is in a header file, which is to be included in an actual application that needs to intersect polygons. If you include this file, it also means you pull in all the using declarations, which could result in unexpected behavior. It might be safe to move the using declarations into class ...

4

Neither of your solutions handles the type of the replacement correctly. For instance "bar.baz.foo:111222" replaces foo:333444 with foo:"111222" where I would expect it to result in foo:111222. Further they can handle if the replacement is an array, but not if it is an object due to the use of split(':'). You should use str.indexOf(':') ...

3

We can simplify this quite a bit. At a high level, note that all you are doing is: Converting each digit of the input to an array of possible neighbors. Now you have an array of neighbor arrays. The answer is simply the cartesian cross product of those neighbor arrays. Sort them. Turn the into back into strings. The cartesian cross-product is a simple ...

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 ...

3

I really don't recommend trying to write anything that is more complicated than necessary during an interview, especially if it's on a whiteboard. Interviews are stressful enough for you; introducing more opportunities for errors is not a good idea. As for the interviewer, their likely thoughts are that your code lacks elegance and is hard to verify. In ...

3

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 ...

3

As you pointed out, you don't actually need to lay the characters out into a grid. It would be more efficient to use arithmetic to figure out the indexes of the characters in each row. For example, if num_rows is 3, then each down-up cycle will consist of 4 characters. (zigzag_size = 2 * num_rows - 2). Then we also know that the top of each zigzag will ...

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