We had training related to java, and was short example of code, like that : void todoSomethig(Agent agent){ if(agent!=null && agent.getName().length() > 0){ System.out.println(agent.getFullName()) } } and all developers tried to find places where NPE can be thrown. Yes, we found that name can be null agent.getName() also that agent.getFullName() can throw NPE. But I asked, next question : Can method length() throw NPE ??? So, here is short description how that can be implemented 🙂…
"Simple puzzle NPE (String)"Author: Hashim
A few days ago my friend asked me a question, How can we find the longest substring which contains 2 unique characters in linear time without additional memory? Yeah, we spoke about algorithms 🙂. I suggested to use 2 cursors like start and end and just get substring by these cursors. Generally idea is right, but how can we manage these cursors ? This is quite hard. Maybe not so hard but need some time to…
"Longest substring with 2 unique chars Algorithm"Recently I’ve read IT topic about interview questions and was surprised, Programmers discussed about “hard” interview question “Reversing of Linked List”. Judging to that topic the task was hard for most candidates and I decided to write short note about Reversing Linked List. Firstly this task was to Reverse Singly Linked List using recursion; Secondly time for creating algorithm was about 5 mins; So, I’ve used Java for realization but you can write the same…
"Reverse Singly Linked List using Recursion implementation"I’ve solved next task Find Minimum in Rotated Sorted ArrayII. The main difference between two solutions is duplication. So, task description: Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the…
"Algorithm {Find Minimum in Rotated Sorted ArrayII}"I’ve just found that leetcode added new one task and decided solve it 🙂. SO, new task is Min Stack. Task description: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) — Push element x onto stack. pop() — Removes the element on top of the stack. top() — Get the top element. getMin() — Retrieve the minimum element in the stack. As you can see we need…
"Algorithm {Min Stack}"