# Tag Info

349

I'm going to tell you the things in your code that made me wonder. # Script that verifies the correctness of a toolbar offers import urllib, bencode , hashlib Extra space after bencode suggests lack of attention to detail. from hashlib import sha1 url = "http://update.utorrent.com/installoffer.php?offer=conduit" filename = "content.txt" f= urllib....

96

Problems I see: My problem with your code is that you are newing a lot of stuff that should just be objects. map<string, int> *lines = new map<string, int>(); multimap<int, string> *lines_by_count = new multimap<int, string>(); Both of these should just be plain objects. map<string, int> lines; multimap<int, ...

69

It can be done in O(n) time. Split your array into two logical parts. Positive and negative. Then apply square to each element in both arrays. Then merge the arrays( merging can be done in O(n) ), but merge the array with previously negative integers in reverse order, since its values will be reversed.

60

Then add a special case In all likelihood, they were looking to see how flexible and maintainable you could make your code be, and you gave them a showcase of some raw technical knowledge instead. I'm not impressed with the fact that the fizzbuzz logic is written in three different places. If you were tasked with adding another "special case" and keep the ...

58

Run your code through pep8 and possibly a more pedantic static analyzer like pylint. You will find these tools don't like some of your formatting and variable names. They will likely complain there are too many variables, branches, etc. because the code is not broken up into modularized functions.

57

While other answers make good points, I have to wonder why you are using recursion. This is such a simple problem to solve with a for loop. I assume that you are not supposed to start from any index other than index 0, so consider the following routine: public int searchArray(int[] arr, int elem) { for (int i = 0; i < arr.length; ) { if (...

48

You're doing manual memory management. That's not a good idea. In fact, that's something that you don't need to do at all in modern C++. You either use automatic objects, or use use smart pointers to dynamically allocated objects. In your case, there's no need to do dynamic allocation at all. Instead of: map<string, int> *lines = new map<string, ...

46

Overall You missed the point. If you are supposed to create a class to track the score of game you did it wrong. Most of the logic that should be inside the class has been left inside main. If you see the pattern: Get info from object. Manipulate data. Update state of object. This usually means that you should have a function called Manipulate() (where ...

45

You get the job done in 30 minutes and the use of a stack is the way to go, so that's a good start. In my opinion you're writing a little too much (repetitive) code and it could be a lot easier to read if you use a switch-statement instead: public bool IsValidReview(string s) { Stack<char> endings = new Stack<char>(); foreach (var curr in s)...

44

I guess what they were looking for was methods on classes. Also I recommend you download resharper and see what it says about your naming conventions as they are non standard. A 'Classic OO' as I call it approach might look a bit like public class Product { public string Name { get; private set; } public decimal BasePrice { get; ...

42

If I was to take your code then multiply it out 100 or a thousand times into other files (the size of a regular commercial/enterprise application then it would be a dog's breakfast and not maintainable at all. You need to take more care with your code and make it readable and consistent. Think of it as formatting your resume for a potential employer. Use ...

39

No one but the folks who reviewed your code for the company can say with certainty why you were rejected. It also depends on the position you're applying for. If you're applying for an entry-level role or a guru role, the expectations are drastically different. As a hiring manager, a couple things jumped out at me (from most to least relevant to me, a ...

37

One of the best ways to make a program faster is not to compute things that you don't have to. In this question you are asked to compute the number of permutations. But your implementation goes and constructs all the permutations themselves, only to throw them all away again. It would be better to count them directly, via some kind of a mathematical formula. ...

36

In an interview it usually doesn't matter if you actually solve the problem. What is most important is the way you (try to) solve it. If they don't tell you it should be the possibly fastest solution ever you should not optimize it prematurely but instead show that you know how to write SOLID code like encapsulate propertly, make the code testable etc. show ...

33

The problem as stated was: There is an sorted array. You need to find all the missing numbers. Write the complete code, without using any generics or inbuilt function or binary operators. First and last terms will be given. Array will be sorted. Array always starts with zero. When I ask questions like this in interviews, one of the things that I'm ...

32

The problem is in $O(n)$. Consider the case that 200_success describes. You have a sequence of alternating 1 and 2's where a single 1 is replaced by a 3. When you are asked to search for a 3 you know, after inspecting the first element, that it will have an even index. But if every odd index holds a 2 then any even index can hold a 3, so you can ...

32

There's actually a bit hack for this : private static boolean isPowerOfTwo(int number) { return number > 0 && ((number & (number - 1)) == 0); } (ref : Bit Twiddling hacks) This exploits the fact, that in binary notation a power of two is a 1 followed by a number of 0's, and the number just below is all 1's equal to that number of 0's :...

31

For one thing, the inconsistency with whitespace use and curly brace placing may demonstrate a lack of attention to detail. Before you attack the actual problem, make sure your code is written cleanly. This: for(int i=0;i<=rowIndex;i++) should use some whitespace: for (int i = 0; i <= rowIndex; i++) You already do this in other places, and it ...

31

I prefer your solution to his. My reasoning is that: yours has constants only in the String manipulation you do not do string concatenation the logic is distinct for each remainder. Both options could be better though. The lack of 'breathing space' (white space between keywords, variables, values, and operators) leads to hard-to-read code. Lines like: for(...

31

I have since been informed that the team was hoping for "A more OO design"...felt like I took it in that direction, personally. All the methods in your DrinkMachine class are static. I believe the ability to have more than one DrinkMachine is a very important feature. DrinkMachines should be able to have different inventories, different products, etc. Now, ...

30

In general First and foremost, you were asked to produce a program that generates a list of 10,000 numbers in random orders. You've added far too much complexity. There's no need for input or output (other than the final output). It's not a good immediate impression to heavily over-architect the program. I would be expecting a few lines of code (5-10), with ...

29

The algorithm you have implemented is known as counting sort. Its run-time cost is linear in the size of the input – faster than any comparison-based sorting algorithm can possibly get. (At the cost of being also linear in the difference of the maximum and minimum element in the input.) Congratulations if you've come up with this idea on your own. Since ...

28

Let's compare your version to the % 15 version: public class FizzBuzz { public static void main(String[] args) { for (int i = 1; i <= 100; i++) { if (i % 15 == 0) { System.out.println("FizzBuzz") } else if (i % 3 == 0) { System.out.println("Fizz"); } else if (i % 5 == 0) { ...

28

There are at least two optimizations you could make. First of all, you are performing r multiplications and r divisions for each value of C(k,r) you compute for r < k/2. You should only need at most one multiplication and one division per value of C(k,r) that you compute, because at the time you want to compute C(k,r) you have already computed and stored ...

27

Problems: I would say that import should be an efficient way of reusing the libraries. import urllib, bencode , hashlib from hashlib import sha1 In the above line of code you have imported hashlib and sha1 from hashlib on the second line. There is nothing wrong in that, but when you just want to use only sha1, then no need to import the entire class. ...

27

This is mostly how I would solve it (in Python): In [2]: import random In [3]: class Deck: ...: def __init__(self): ...: self._cards = list(range(52)) ...: def shuffle(self): ...: random.shuffle(self._cards) ...: def deal(self): ...: return self._cards.pop() The data structure is as simple as it gets, ...

26

As an interviewer, I want to see candidates demonstrate the skills I'm hiring for. In particular, I want to see unit tests, because I want you to write tests for your code if you come work for me. If you had written unit tests for your code, you probably would have caught many of the items listed in the other answers. You at least would have demonstrated ...

26

Java 8 has good support accross all IDEs but there are still a few differences. For example your code, as-is, doesn't compile with the latest Eclipse version (Mars.2). On the command line with JDK 1.8.0_51, it compiles fine. I would hope they didn't reach out to you because they tested it with Eclipse. (Since I'm using Eclipse, I tweaked it a little to ...

26

You don't need to calculate the expected sum in a loop since you can use the formula: $S = (a_1 + a_n) * n / 2$ Also it makes sense to use LINQ to get the actual sum: private static int MissingNumber(int[] numbers, int min, int max) { int expectedSum = (min + max) * (numbers.Length + 1) / 2; int actualSum = numbers.Sum(); // I do realize ...

26

I'm going to give an alternative approach to tchbot's answer There are places where SOLID prinicpals are required. I don't see this as one of them. Here, we have a simple "Are you capable of writing a loop" question - about the same level as the Fizz Buzz test. Really, the function is described in one line, and when applying mathematical formulas, I ...

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