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


14

Here are some things that may help you improve your code. Use all required #includes The code uses memcpy, so it should #include <string.h>. It might still compile on your machine, with your compiler, but it's not portable. Think about potential errors As one of the comments correctly notes, if one of the entries has the value of zero, this line will ...


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


9

TL;DR private String increment(String input){ char[] chars = input.toCharArray(); boolean stopLoop = false; for(int i = chars.length - 1; i >= 0; i--){ char character = chars[i]; switch (character) { case '0', '1', '2', '3', '4', '5', '6', '7', '8' -> { chars[i] = (char) (character + 1); ...


8

def checkPalindrome(self,lst): return lst[:] == lst[::-1] This is an extremely expensive implementation. lst[:] is needlessly creating a copy of lst, then lst[::-1] creates a second complete copy of lst (but reversed). There are many ways of approaching this, but I would go for something like this: def checkPalindrome(self, lst): return all(start == ...


8

Take full advantage of the language. You ask whether one should use handy built-in functions and methods during a coding interview. Absolutely! Interviewers want to see, among other things, that you are fluent in the language(s) where you claim fluency. Write natural, idiomatic code -- not code you think the interviewer wants to see. There can be some pretty ...


8

Because this is an interview question, the most important part is the questions you ask. The questions show that you understand that requirements are seldom complete and know what kind of limitations affect the implementation of the algorithm. The ones that need to be asked here are: How long are the input strings of typical input? How long are the numeric ...


8

long both; Using a long for storing a cast pointer can be both wasteful and too little. Just use the dedicated uintptr_t. Even if your implementation might not be fully up to C99 (MS greets), it probably has <stdint.h> and the typedef. pStNode newNode = (StNode *) malloc(sizeof(StNode)); The above line demonstrates three bad ideas: More pieces to ...


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


7

As mentioned by a commenter on the question, neither version satisfies the requirement to return a new array. I'll leave that for you to fix yourself (demonstrating your understanding of memory allocation to your interviewer). The division version requires some modification to work when one or more inputs are zero. I suggest keeping track of the position ...


7

long isn't necessarily a good choice of integer type for storing pointers - luckily <stdint.h> provides us with uintptr_t which is guaranteed to be wide enough for this purpose (always prefer an unsigned type when working with bitwise operations). I dislike hiding pointer types behind typedefs such as pStNode. I think it's clearer to use the pointer ...


7

Hard coded You have created yourself a future problem by hard coding the data in your code. When dealing with taxes (or any business logic) there is one certainty, and that is change. With the data embedded in the code, making even a minor change will take some effort and will be very prone to error. You need to create a data structure that allows you to ...


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


6

The HashSet<int> won't be changed hence make it readonly. Instead of calling Contains() prior to the call to Add(), if this evaluates to false, can be simplified to just return _set.Add(val); because the Add() method returns false if the value is allready in the HashSet. Reference Instead of calling Contains() prior to calling Remove() can be ...


5

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


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


5

static String padLeftZeros(String inputString, int length) { Having no modifier specified means that it is package-private. Personally, I like to pretend that package-private does not exist, as it easily leads to "hidden" coupling between classes, and makes it harder for extending classes to actually utilize the classes. On top of that, you can ...


5

Test case structure It doesn't seem like the code totally "lacked effort and structure" but the structure, at least of the test cases, could be better. Since all three describe() blocks contain a test that calls getLatlonFromPostcode so there could be a single describe() block with three it() blocks to test the various aspects of how the function ...


5

The code could be improved with a few tweaks, but it's not bad, and the overall structure looks just fine to me. Error handling is in the wrong place Some of your route handler are async, and inside them you await. Whenever you have a Promise, make sure to handle possible errors properly. Although the functions outside of your router handlers handle errors - ...


5

You could eliminate the special case here: //left most element special handling new_arr[0]=1; //swipe up for(int i=1; i<arr_size; i++) { mult_prefix *= nums[i-1]; new_arr[i] = mult_prefix; } by assigning before multiplying, and bringing index 0 into the loop: //swipe up for(int i=0; i<arr_size; i++) { new_arr[i] = mult_prefix; ...


5

Regarding your PositionTransformer comment : We have an 8x8 board where columns are represented by letters and rows by numbers (it might be the opposite I don't really play chess). But that doesn't matter to your code. What the code is working with is a 8x8 grid. However, your user interacts with the board with letters and numbers. What this means is, ...


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

I have some suggestions for your code. Replace the for loop with an enhanced 'for' loop In your code, you don’t actually need the index provided by the loop, you can use the enhanced version. before for (int i = 0; i < a.length; i++) { //[...] } after for (int current : a) { //[...] } Variable and parameter name should be in camelCase style The ...


4

I'm assuming that the code works and fulfills all the usability requirements that you got. Also assuming that this was a home assignment with "unlimited" time where you could code at your own pace and no one was hanging over your shoulder. To summarize, I think there are a bunch of "red flags" showing poor habits and/or poor awareness of ...


4

After some tests, the code does look correct, but the logic takes some time to parse and understand, which is a significant disadvantage; when writing good, maintainable code, readability is usually the most important factor to consider. If a script is running too slowly for your liking, you can run a performance test to identify the bottleneck(s) and then ...


4

From a performance point of perspective, IsSubsequence will always be faster than IsSubsequence2, because it only needs to iterate 2 arrays simultaneously and has no further allocations on the heap. If you want to improve on performance and also make sure, you never get a StackOverFlowException, you should get rid of the recursion in your Helper method and ...


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