Interview questions: verify the braces

This is one of the easier coding tasks, but you still can meet it in some preliminary tech screening. The problem looks like this:

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Description taken from Leetcode (c).

How do you solve it? Continue reading

Posted in interviews, java, Programming, Uncategorized | Tagged , , , | 4 Comments

Cover your bases, or How to play the tests

Writing tests is an important part of a developer’s job, however, it can be boring. Especially if you are not a TDD fan and leave the tests for after the code is done, so that you have to write a big batch of unit tests at once. How to break the monotony and add at least some element of challenge to this routine? Enter running tests with coverage. Continue reading

Posted in java | Tagged , , | Leave a comment

What programmers want

Recently I’ve been asked by an HR person to tell which factors influence Java programmers wanting to change a job, what is important when we consider a change. I gave it a great deal of thought, and the result has been so extensive as to warrant a blog post. Which I now present here.

I don’t aspire to cover everything or to be completely objective, but I’ve tried to think of everything that can be important from my point of view. So, here goes everything. Continue reading

Posted in HR, Programming | Tagged , | Leave a comment

There’s something about… ThreadPoolExecutor

There’s definitely a lot to say about Executors in Java and ThreadPoolExecutor as a particular case, but what I recently ran into was a simple issue. An application gets a large stream of data from source to destination; it does so concurrently so as to speed up the process. Problem was, when I added the parallelism, the application started running out of memory very soon.

Well, what do you do when you have an OutOfMemory exception?

You profile!

Using jvisualvm and extended logging I found out the culprit. One of the stages of the data preparation process, after it’s been read from the source, was slower than others. So, the thread pool with tasks that I had was quickly growing and eating up my precious memory.

Well, the pool I used was supposed to be fixed, and it was created like this:

ExecutorService pool = Executors.newFixedThreadPool(threadCount);

But reading the documentation again (always an exciting thing to do!), we find the following:

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available.

What does it mean?
It means that the pool won’t process more than a fixed number of threads at a time, but what it won’t process it will keep submitting into the wait queue, and the queue is unlimited.

Great, you say, let’s limit the queue!

        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(maxThreads);

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadCount,





return threadPoolExecutor;

We run the app again and this time, we have a nasty exception as soon as the pool reaches its limit: a RejectedExecutionException.
What has just happened?
Well, the pool can’t accept new tasks because the queue has no space left, and so it rejects them.
What we need here is basically to have a way for the calling thread to not submit the task while there’s no space for it, i.e. make the method, submitting to the pool, to be a blocking one.
But how?
Turns out there’s a way to do it without complex workarounds, though maybe not a very direct one: a rejection policy.

We can set a RejectedExecutionHandler for the thread pool. There are four standard policies:

Seriously, I can’t imagine why anyone would want to use #3 and #4. As to the #1, we have just seen it in action and didn’t like it. #2, however, looks promising. If the calling thread is running the task on its own, then it won’t try to submit anything else until it is done, right? And hopefully this will give the pool some time to offload the tasks.

        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(maxThreads);

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadCount,





threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

return threadPoolExecutor;

And it turns out that this really works as expected.

Posted in java, Programming, Uncategorized | Tagged | Leave a comment

This is just Groovy, or stubbing chained method calls

This is just a quick note to self because I am prone to forget such things. This blog is my reminder right? I am writing down the things that may be of use later.

So, today I spent a few hours trying to write just one test. For this chained method call:

        FindIterable<Document> ops = oplogCollection.find(filter)
                .sort(new BsonDocument().append("$natural", new BsonInt32(1)))

We are writing tests in both Groovy and Java, but this one was in Groovy. I was trying to stub the invocation so that it would be used in all the chained calls. And I was failing.
First I tried to use this kind of stub:

        FindIterable<Document> stub = Stub(FluentIterable)
        stub.sort(_ as Bson) >> { Bson filter ->
            // do whatever
            return stub
        stub.batchSize(_ as int) >> { int size ->
            // do whatever
            return stub

So, I declared a stub and I returned it all the time. No luck! Only the first chained method was called.
Then I thought, what if we try to return this instead? Unfortunately, in this case this point to the instance of your test class and not your stub, which I promptly learned.

So, I turned to the best authority.
Google and StackOverflow.

In the end, this is how it worked:

        FindIterable<Document> batchResult = [
                batchSize: { int size ->
                    return tmResult
                }] as FindIterable<Document>

        FindIterable<Document> sortResult = [
                sort: { Bson sort ->
                    return batchResult
                }] as FindIterable<Document>

So, the principle is that you have to create another stub for a chained method call, and then they get chained.
I live and I learn.

Posted in java, Programming | Tagged , , | Leave a comment

ElasticSearch for dummies, or why you can’t find what you just indexed

So, let’s say you are starting to use ElasticSearch. You have created a new index and put some data there. Then you go and try to search by one of the fields… and can’t find a thing.

How can this happen?

Well, one of the reasons is that you probably don’t have mappings.

If you just created an index with no specific settings, then mappings are inferred, as well as analyzers. There are three ways of analyzing a field:

  • analyzed. This is the default. It means that field content will be analyzed and available for full-text search.
  • not_analyzed. This means that field content won’t be processed, but it will be searchable. It will be stored “as is” and you will be able to query it by value.
  • no. It means the field won’t be indexed and you can’t use it for search.

So, the default value is analyzed, which means that all the fields are searchable by default. Well then, why aren’t they searched? Continue reading

Posted in ElasticSearch, Programming, Uncategorized | Tagged , | Leave a comment

Organizing your mind: books to Trello

I have a lot of books to read.

Actually, I have read a lot of books, too. I am always reading. When I was a kid, the easiest way to shut me up and give my parents some peace and quiet was to give me a book. At first it was fairy tales, then myths and legends, adventure, science fiction, fantasy etc. And now that I am all grown up and have to take care of my professional education, it’s often programming books. Though I can’t pretend to have given up my love of fiction. How could I not have read Game of Thrones? (Twice. And will probably be going on the third time if Mr. George Martin continues to hold up on the next book.) How could I have not read Harry Potter? (I am not saying how many times.) How could I not read… whatever, I think the point is obvious here. Continue reading

Posted in Learning, tools | Tagged , | 2 Comments