FANG obsessive Topics: Strings, StringBuilder, and Arraylist!




By Rohit Jha       November 15, 2020




By Rohit Jha

November 15, 2020

LOL! Yes, you heard right FANG‘s obsessive topics or we can say some topics from the competitive programming which FANG is so obsessed with! these days. It has been a past record for the last couple of years Google, Facebook, Microsoft, Netflix, and Amazon have kept on asking questions over these three topics: Strings, StringBuilder, and Arraylist. It is because of their specialty and a lot of inbuilt functions due to which they can customize a lot of questions and can check how logically their deserved software engineer can think!

How surely we can say that?

We, ourselves gave the online coding rounds of Google, Goldman Sachs, and Microsoft, and our other mates gave the exams of Facebook and Netflix. So after all the observations, we concluded this fact that these Top Notch product-based companies are very obsessed with asking questions over recursion, dynamic programming and graphs which we will be covering in our upcoming posts. Mostly those questions of recursion, DP, and graphs were based on string operations, StringBuilder, and ArrayList. So, this our main reason to give a brief idea to head start with these topics and make our readers land in their dream company! <3

So, Here we go!

Strings

It is basically a type of character-type-array. For those who don’t know about array. In our programming languages, an array is a set of similar elements stored under one name and contiguous memory. or we can say making a group look-alike elements with the same characteristics :P. Any word or set of characters put under doubles quotes is a string. Now it’s up to us how we use it and how we are manipulating it.

In C++ an array is declared as :

TypeOfArray name[size of elements]; // int meusec[5];

In Java we declare it as: Int[] meusec=new int[size];

But Strings can be declared in a number of ways, in this post we will be using and declaring everything in JAVA programming language (because it’s sweet! just trust us)

Declaration:

Type 1: (initialization by coder)

Type 2:(asking input by user)

Type3:(using new keyword)

For proper implementation of Strings in programs click here!

Java has some special feature for strings such as interning and immutablity!

Interning

Before assigning separate memory to a string, JVM (Java Virtual machine) first looks for the actual string value in the memory and if it finds them, it passes the same address to that variable. This thing is done in order to save space or optimize the memory. for example:-

And now if we declare a new variable

Immutability

This feature tells us that, the reference is always mutable but the instance is not. whenever we create a string its instance or its value becomes unchangable. So in actual if we ever change the value of a string by just assigning it to some new value, generally in the memory what happens is that -> the variable changes its reference from the earlier value(stored in heap) to newly assigned value with new address. Due to this, we can say that immutability is due to the interning concept in java and hence it impacts in slower performances in many cases.

So just to avoid this performance issue, we use Stringbuilders!!!!

StringBuilders

These are special types of Strings with mutability feature and have better performances in comparison to normal strings classes. Earlier in Strings we could barely perform any good operations such as finding length , getting a character at an index. But in StringBuilders we are given with lot of different functions to make changes in the same string in O(n) time complexity.

Declaration:

Now, Given below are the codes and major functions to use the stringbuilders:

FANG obsessive Topics: Strings, StringBuilder, and Arraylist! 2

Output corresponding to the above code:Strings_meusec

StringBuilder is more like the ArrayList which we are going to discuss next. In StringBuilder, if we add an extra element it suddenly doubles its size.

ArrayList

ArrayList is mostly similar to arrays but the major difference is that it is resizable, you can easily remove any element or add extra elements from the list. Moreover, it comes with some extra functions as well, which makes the coding rounds and interview typically difficult also. So in the below code, I have given a brief implementation of all those inbuilt arraylist functions.

strings

And the output related to the above code is given below:

strings

Get the latest tech news and updatesethical hacking tutorials and cybersecurity tips and tricks. Check out MeuSec for more.


You Might Also Like

Subscribe to our Newsletter

Advertisement