Featured

Understanding Git Basics

Git 101

One common requirement which occur quite often in all the Software Engineering job description is Git. Git has become an essential skill for both developers and testers. So lets see the basics of git and how to get started with it.

Image for post

Git is a Version Control System. A version control system is a software that manages all the changes that occur in a code over a period of time. Whenever a file is edited, it makes a copy of the file and save it as checkpoints. If any of the edits needs to removed or rolled back, these checkpoints becomes incredibly useful to go back to last saved state and proceed from that. Its like a eraser. There are many version control systems available in the market. However, git has gained the spotlight , thanks to its “distributed version control” feature.

So, What is a distributed version control?

There are two types of version control.

  • Centralized Version Control
  • Distributed Version Control

Centralized Version Control

Image for post

Concept — There is only one central copy of the project in the server. All the commit is performed to this central copy.

How it works

  • Pull down the changes if any from the central server.
  • Make your updates and test it for bugs
  • Commit your updates to the central server for other programmers to see.

Distributed Version Control

Image for post

Concept

Each programmer has his own copy of the project and will work on it in the local repository.

How it works

  • Instead of doing only the pull down of changes from the repo, the programmer clones the entire repository from the server to his local repository.
  • Each programmer works on his local copy of the project, updating it by creating a branch.
  • Pull request is initiated to perform team work on the updates.
  • Once the updates are approved, push commit is used to update it in the server.
  • In case of the server fails, any of the client repositories can be copied back to server to restore it.

Basics Terminologies

  • Repository — It is a directory or a folder used to store your projects. There are two repositories

1. Local Repository — This is your local copy. The changes and updates you do to the files wont be reflected in the server until you do push and commit.

2. Remote Repository — This is repository in the server that can be viewed and accessed by all.

  • Push — It is used when you want to update the project in the remote repository
  • Commit — Submitting the files in the local repository. This is more like a check in or save option. If you are from SQL background you would be familiar with this.
  • Pull — It is used to download the latest update or the copy of the project from the remote repository. This command usually downloads the file from the remote repository and merge it with the file in the local repository.
  • Master — When you create a repository, git creates a branch and by default its called master. Master points to the commit you have made in the project and it always points to the latest commit.
  • Stage — This is one term you will often come across while researching about git. As simply as it can be explained, staging is a phase before the actual commit. It prepares the file for the final commit. Metaphorically speaking, imagine a product, say vegetables. The farm where they are grown is your local repository. The store to which they are brought to sell is the staging phase. You buying it and taking them home is the commit.
Image for post
  • Origin — This is the link between your local and remote repository. It is like a watermark. When you clone a remote repository , git creates a connection called origin, which refers to the remote repository which was cloned.
  • Clone — As simple as the name itself, it means copy. When you click clone, you are creating a mirror image of the repository into your local machine.

So, you have created a GitHub account and have created a repository. Now you want to clone it, but you see the options “Clone” & “Download”. Whats the difference between clone and download. Well, clone creates a copy of the repo into your local machine with all the git files.You can work on those files, see the history of commits, update the files and push it to the remote repo and much more. Download on the other hand simple copies the files without git. You can only see the latest commit. You cannot develop further and update it in the remote. It is like your copy and thats it.

Problem Statement- Palindromes

One of the most common subject which is often questioned in many coding interviews is the topic of a palindrome. So let’s discuss a couple of problem-solving methods for some palindrome based questions.

What problem statements are we discussing?

  • Given a number, determine if its a palindrome?
  • Given a singly linked list, determine if it is a palindrome.

So, what is a palindrome?

Palindrome can be a word, phrase or a number that can be read and spelled the same way in either direction.

19 Words That Are the Same Backwards and Forwards
Courtesy: Reader’s Digest`

Generally, palindrome based coding questions can be tested on integers, strings, linked lists, stacks etc

So let’s start from the most basic palindrome question:

Given a number, determine if its a palindrome?

This means if the input is “2332” the output should be true. Because if you reverse the number “2332”, it would be the same as the original.

So before we dive into coding, let’s discuss 3 main logic behind the following code.

  1. Negative numbers cannot be a palindrome.
  2. The last digit of a number can be obtained by determining the modulo of the number.

3. We are going to iterate through the number until the original number is greater than 0.

4. While loop: In each iteration, the modulo of the number is determined and the last digit obtained is added to the variable “reversed_integer”. Finally, the current last digit is removed from the original number by dividing it by 10.

5. If Condition: Finally we check for equality between the original number and the reversed number.

For E.g

Input : 2332

Problem Solution:

public class palindrome
{
public static void palindrom(int n)
{
int orginalNumber,reversedNum=0,lastDigit;
orginalNumber=n;
while(orginalNumber!=0)
{
lastDigit=orginalNumber%10;
reversedNum= reversedNum *10 + lastDigit; orginalNumber=orginalNumber/10;
}
if(reversedNum==n)
{
System.out.println(“It is a palindrome”);
}
else
{
System.out.println(“It is not a palindrome”);
}
public static void main(String[] args)
{
int x =121;
palindrom(x);
}
}

Explanation:

Iteration Steps

Iteration 1 : lastDigit=1 ; reversedNum=1 ; orginalNumber=12

Iteration 2 : lastDigit=2 ; reversedNum=12 ; orginalNumber=1

Iteration 3: lastDigit=1 ; reversedNum=121 ; originalNumber=0

<End of Iteration as while condition fails>

If look now checks if the originalNumber is same as reversedNum and prints the result.

Output:

It is a palindrome

Problem 2 :

Given a singly linked list, determine if it is a palindrome.

First, let’s discuss some basics of linked lists.

Definition: Linked List is a data structure that stores a collection of items. Imagine a chain when you try to visualize linked lists. Each ring is called “Node”. Each node has two sections

  • Data.
  • Address of the next node.
Image for post
Singly linked list

Question1: So how are they stored in the memory? Dynamic memory allocation.

Unlike an array, the nodes of the linked list are not stored in a consecutive manner. The best non-technical analogy would be the game of treasure hunt. Each clue paper you find is the data and clue written on it is the memory address of the next node.

Question 2: Can elements of a linked list contain duplicate data? Very important question especially if your problem statement is a “Palindrome”.

The answer is yes. A linked list can have duplicate items. While performing insertion, it doesn’t check for duplicate data.

Question 3: Lastly, can a linked list contain data of different types? Say, the first node as an integer stored in it, Can I store a character in the second node?

Yes, it is possible although you have to make sure the list is declared as List<Object> or List<Serializable>.

What is serializable — well this is topic is huge and it is out of the scope of this post. But I can tell you the basic idea behind serialization. Serializable is used to convert an object(Instance of a class) into bytes so that it can be written to a disk or can even be passed over the network.

Hard-disks or network components are hardware components. And these components don’t understand linked lists or any other data structures. So the object needs to be translated into a type that these hardware components can understand and that type is bytes.

It’s like visiting China without knowing Chinese. You need someone to translate the language for you for better communication. Serializable does this job. It converts the object into bytes and store it in the hard disk or sends the data across the network.

A linked list is much more than what is discussed above and I would suggest you read more on the topic of a linked list while preparing for an interview.

Now going back to the problem statement.

Image for post

Problem solution

//Definition of Node //

public class linkedlist_palindrome
{
Node head;
static class Node
{
int data;
Node next;
public Node(int d) {
data=d;
next=null;
}
}
// Function that chess for Palindrome //
public static void findmid(linkedlist_palindrome splitlist, Node h)
{
Node secondhead;
Node fastpointer=h;
Node slowpointer=h;
while(fastpointer!=null && fastpointer.next!=null)
{
fastpointer=fastpointer.next.next;
slowpointer=slowpointer.next;
}
secondhead=slowpointer.next;
slowpointer.next=null;
Node n1 = secondhead;
Node n2 = n1.next;
while(n1!=null && n2!=null){
Node temp = n2.next;
n2.next = n1;
n1 = n2;
n2 = temp;
}
secondhead.next = null;
//compare two sublists now
Node x= (n2==null?n1:n2);
Node y = h;
while(x!=null)
{
if(x.data != y.data)
System.out.println(“This is not palindrome”);
x = x.next;
y = y.next;
}

Problem Logic:

There are many ways you can find a palindrome in a linked list.

The above solution logic has the following steps,

  1. Split the linked list into two parts. This is done by finding the mid of the list and elements beyond the mid node are considered as the second list. In case of even number of nodes, there will be two mid nodes and the second mid node will be considered as a mid node.
  2. The second part of the list is reversed.
  3. The two lists are compared for equality to determine if the list is a palindrome or not.

The most important part of the above logic is to split the list into two parts. This is done by defining two pointers, one slow pointer and another fast pointer. The slow pointer points to the head and the fast pointer points to the second node. The loop iterates until the fast pointer reaches null. When the fast pointer reaches null, the slow pointer points to the mid value.

In the above example, 3 will be mid-node.

Output

Yes, it is a palindrome

Introduce Yourself (Example Post)

This is an example post, originally published as part of Blogging University. Enroll in one of our ten programs, and start your blog right.

You’re going to publish a post today. Don’t worry about how your blog looks. Don’t worry if you haven’t given it a name yet, or you’re feeling overwhelmed. Just click the “New Post” button, and tell us why you’re here.

Why do this?

  • Because it gives new readers context. What are you about? Why should they read your blog?
  • Because it will help you focus you own ideas about your blog and what you’d like to do with it.

The post can be short or long, a personal intro to your life or a bloggy mission statement, a manifesto for the future or a simple outline of your the types of things you hope to publish.

To help you get started, here are a few questions:

  • Why are you blogging publicly, rather than keeping a personal journal?
  • What topics do you think you’ll write about?
  • Who would you love to connect with via your blog?
  • If you blog successfully throughout the next year, what would you hope to have accomplished?

You’re not locked into any of this; one of the wonderful things about blogs is how they constantly evolve as we learn, grow, and interact with one another — but it’s good to know where and why you started, and articulating your goals may just give you a few other post ideas.

Can’t think how to get started? Just write the first thing that pops into your head. Anne Lamott, author of a book on writing we love, says that you need to give yourself permission to write a “crappy first draft”. Anne makes a great point — just start writing, and worry about editing it later.

When you’re ready to publish, give your post three to five tags that describe your blog’s focus — writing, photography, fiction, parenting, food, cars, movies, sports, whatever. These tags will help others who care about your topics find you in the Reader. Make sure one of the tags is “zerotohero,” so other new bloggers can find you, too.