A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Hot answers tagged

6

Document your code. In the code. • what use is MovingAverage? • is size fixed for the lifetime of any given MovingAverage instance?  I don't know even scrutinising the code: size is neither final nor private • what does next(int value) return? accumulate ints into longs - even the sum of two ints can overflow. program against interfaces - and give ...


3

As has been pointed out by @Joop Eggen, you should decide if Node is part of the interface or not. As it stands, you can directly access the value of head and tail via the accessors (getHead,getTail), however you can't get at any of the other values in the other values you've stored in the LinkedList unless you happen to have kept track of the Node that you ...


3

Indeed the cases for delete can be reduced. public void delete(Node node) { Node prior = null; Node current = head; while (current != node && current != null) { prior = current; current = current.next; } if (current != null) { if (prior == null) { head = current.next; } else { prior.next =...


3

Graph is a strange name for something designed to manipulate friendships. As indicated by @greybeard in the comments, it doesn't really represent anything at the moment. print I hate seeing methods like printFriends on classes. They very rarely belong. If you want to have the class provide a method/toString implementation that can be used by it's ...


3

I'm going to assume this question is in the context of quality interview code, not quality production code. Websites like GeeksForGeeks (https://practice.geeksforgeeks.org/problems/jumping-caterpillars/0) don't grade you based on production code quality. Your failure to correctly spell the most important word in the problem domain (Caterpillar) would put ...


3

I have some suggestion for you. 1) Constant name should always be uppercase. private static final Scanner SCANNER = new Scanner(System.in); 2) When creating arrays, I suggest that you use the java style instead of the c style, since it's the more used and less error-prone, in my opinion. Before public static void main(String args[]) { //[...] } ...


3

Attention to Detail What seem like trivial mistakes can be interpreted as a lack of attention to detail. So whilst your interface has a simple typo claculateUnitForSingleDay vs calculateUnitForSingleDay, it is noticeable. You don't want this slipping into production code, particularly not on an interface or an API object because you can find yourself ...


2

Your code has a bug. I can reproduce it fairly consistently by changing your run method to: @Override public void run() { try { for(int i=0;i<100;i++) { uniSexBathroom.occupy(person); } } catch (InterruptedException e) { e.printStackTrace(); } } Notice, that I've increased the number of times that each ...


2

Preliminaries: there's strategy, and there's tactics. A somewhat common procedure to tackle performance problems is to look at "inner" loops first - not entirely wrong, but the golden rule is measure. (And, when turning to others for support, provide measurement results and a test data generator or test data.) Some statements regarding edit distance: • ...


2

In isAnagram one should start local variables with a small letter (just a convention). Do not use if-else to return a boolean. public static boolean isAnagram(String str1, String str2) { String s1 = str1.replaceAll("\\s", ""); String s2 = str2.replaceAll("\\s", ""); boolean status = true; if (s1.length() != s2.length()) { status = ...


2

Interface There's a name conflict with java.util.map I'd try to avoid using names that are the same as existing library classes. Does print really belong on this interface? If you really wanted to have it as part of the implementation, it could be there without needing to be on the interface. However, it would be better to provide some kind of iterator (...


1

Based only on the code, I have suggestions: For the variable, I suggest that you mark them private to prevent exposure; especially the collections. private Queue<Integer> myQueue; private int size; I think the comment on the constructor is useless. You can remove the type java.util.LinkedList in the diamond operation, in the implementation ...


1

As said by @TorbenPutkonen, if you have the ownership on your dao classes then you can parameterize them. abstract class Repository<X> { private LiveData<List<X>> allEntities; private Dao<X> dao; void insert(X entity); } abstract class ListingFragmentViewModel<X> { private Repository<X> repository; public ...


1

You seem to have started down a singleton approach, then changed your mind / not completed it. private static DbConnectionPoolManager instance; public static DbConnectionPoolManager get(DbPoolconfig dbPoolconfig) { if (instance == null) { synchronized (DbConnectionPoolManager.class) { if (instance == null) { return ...


1

A few subjective points... Isolation Going forward, you're going to want to isolate the UI from the algorithm as much as possible. So, using methods to break up the algorithm as suggested by @Doi9t is definitely worth considering. I think it's also worth considering making the output stream a class field for the moment. This is going to be closer to the ...


1

A potential issue is related to the static Scanner object: private static Scanner scanner = new Scanner(System.in); This scenario can bring potential problems due to the closing of the Scanner resource left to the developer. From Java 8 it is possible to use the try-with-resources statement to ensure closing of resources (in this case the Scanner object to ...


1

Care about final Your parameters are never modified, so it is good practice to mark them final. Probably the method too, unless you expect it can be overridden. public final int shortestDistance(final String[] words, final String word1, final String word2) Compare by indices Instead of your for loop, you can use the indices of the elements. public final ...


1

Parameter for sorting the data (ASC, DESC) If the task actually used 'ASC', 'DESC', then I'd say that there's a good chance they were expecting you to use an enum to make it easy to select the sort type. If not, then personally I'd have preferred a boolean for the ascending parameter. Using magic numbers like '1' to indicate sorting direction is going to ...


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