Categories
Programming & Dev

FANG obsessive Topics: Strings, StringBuilder, and Arraylist!

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)

  • String a;
  • a=” hello”;

Type 2:(asking input by user)

  • String a=in.nextLine(); // taking -> Scanner in=new Scanner(System.in);

Type3:(using new keyword)

  • String s3=new String(“hello”);

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:-

  • String S1=”hello”;
  • String s2=”hello”; // now in this case, In memory/RAM , the two different variables in the stack are referencing to the same address is created in the heap
  • FANG obsessive Topics: Strings, StringBuilder, and Arraylist! 1

And now if we declare a new variable

  • String s3=new String(“hello”); // by this line a new variable will be created in the stack with the new address lets say ‘5k’ referencing to a new object shell referencing to same character array to save space.
  • strings
  • Now, if we do a comparison then by normal ‘==’ sign it will only compare the addresses of two variables and not the values of two variables, hence on doing s1==s2 it will give true but will give you error on s1==s3. But if you use the inbuilt ‘equals’ functions then it will give you true, because it compares the string values of two variables.
  • s1.equals(s3); // will give you true!

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:

  • StringBuilder exp_name =new StringBuilder(“hello”);
  • StringBuilder exp_name =in.nextLine();

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.

Sometimes we include links to online retail stores and/or online campaigns. If you click on one and make a purchase we may receive a small commission.

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *