Data Structure and Dart Programming language problems

  • Home
  • Data Structure
    • Array and String
    • Linklist
  • Google Flutter
    • Google Flutter
    • DART
  • About Us
  • Home
  • Data Structure
    • Array and String
    • Linklist
  • Google Flutter
    • Google Flutter
    • DART
  • About Us

Wednesday, February 15, 2017

Arrays-String interview question 2

 Tech host     3:51 AM     array, array-interview-question, cracking-the-coding-interview, frequently-asked-question, interview-question, popular-string-question, string, string-interview-question, string-palindrome-question     No comments   

Cracking the coding interview

Arrays and String solutions

Hey guys i am providing the solutions for the question given in the Chapter one Arrays and String of the very famous book Cracking the coding interview. All these solutions are written by myself and checked for all the possible cases. Please take a look at them and help your self to solve your problems. If you have any query regarding the solutions or you want to ask something leave your comment and i will get back to you as soon as possible. Happy coding.

Question 2

Given two strings, write a method to decide if one is a permutation of the other. that means that they same number of characters in it but arrangement will be different or we can say that order is different.

Solution
  • You can find the solution for this problem here.
  • Very simple problem just sort the strings and then compare them.
  • Sorting the string means that java will use their ascii value for the comparison process and arrange them in the increasing order and after that you can compare strings to be equal or not.
  • As permutation means that you have the same characters in the string but they will be arranged in the different order sorting them will put them in the increasing order of their ascii value.
Remember this trick as it will be very help full in solving these type of questions in the interview but pretend that you have thought about it during the interview otherwise you might have to face more difficult question that you haven't heard of.
If you find this code hard to understand then please start writing your own code and use pen n paper for that so then you can keep track what is your code all about and what it is calculating.
This problem is one of the most asked question in the interviews so try ti understand this throughly and try similar problems like it.
I wish best of luck to you for your future.
Solution 1 using java.
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */

class Ideone
{
  public static void main (String[] args) throws java.lang.Exception
  {
   System.out.println(permutation());
  }
  public static boolean permutation()
 {
    String str ="asdfghj";
    String str1 = "jhgndsa";
        char[] str2 = str.toCharArray();
        char[] str3 = str1.toCharArray();
    Arrays.sort(str2);
    Arrays.sort(str3);
       str = new String(str2);
       str1 = new String(str3);
       return str.equals(str1);
 }
}

Note that this approach is not only specific to this type of question you can use this sorting technique in various questions such as if you want to check whether to word are anagrams of one another or not. You can simply sort them and compare them to find out. This approach is also helpful in other questions so i suggest that its better to learn this tricks and keep in mind while attempting the question you may find it useful. You can always search for more coding problems like this to get comfortable to this to approach or you can find new problems where this approach is applicable. So go and search more problems like this and check whether this approach is useful or not and you can tell me about it by commenting and writing about this to me.

Try more and more problems that you find on string and arrays and try to develop an approach the will help you understand problems rather then learning the solution. Once you know how to approach the problem you will able to solve any problem related to strings and array. I wish best of luck to you for your future.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Tuesday, February 14, 2017

Arrays-String interview question 1

 Tech host     4:00 AM     array, array-interview-question, cracking-the-coding-interview, frequently-asked-question, interview-question, popular-string-question, string, string-interview-question, string-palindrome-question     No comments   

Cracking the coding interview

Arrays and String question solutions

Hey guys i am providing the solutions for the question given in the Chapter one Arrays and String of the very famous book Cracking the coding interview. All these solutions are written by myself and checked for all the possible cases. Please take a look at them and help your self to solve your problems. If you have any query regarding the solutions or you want to ask something leave your comment and i will get back to you as soon as possible. Happy coding.

Question 1

Implement an algorithm to determine if a string has all unique characters.Think what you can do if you cannot use additional data structures? means that all the characters in the string must be for one time only.

unique-string
Solution
You can find the solution for this problem here

Here i am providing solutions for this question using different data structures to give to the knowledge about the them and to let you know that same problem can be solved in many different ways using different approaches that might be helpful to remember in your interviews.

If you find this code hard to understand then please start writing your own code and use pen n paper for that so then you can keep track what is your code all about and what it is calculating.

This problem is one of the most asked question in the interviews so try ti understand this thoroughly and try similar problems like it.

Solution 1 using hash map java (Worst case Complexity N)

This solution provides you the knowledge about using the hash map and functions of the hash map. Hash map is a data structure that has special key value mapping and used in many problems to reduce the complexity of the algorithm.

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
 public static void main (String[] args) throws java.lang.Exception
 {
  // your code goes here
  System.out.println(unique());
 }

  public static boolean unique()
{
  String str ="asdfjgh";
  HashMap < Character,Integer > map = new HashMap < Character,Integer > ();
  for(int i=0;i < str.length();i++)
  {

    if(map.containsKey(str.charAt(i)))
    {
       return false;
    }
    map.put(str.charAt(i),1);
 System.out.println(map.get('a'));
  }
return true;
}
}
Solution 2 using array java(Worst case Complexity N).

This method uses Boolean array to represent all the character and the value of the Boolean if the particular alphabet occurs one time in the string and that how this solution works checks if the particular alphabet has occur twice or not if yes the return false as the answer

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
 public static void main (String[] args) throws java.lang.Exception
 {
  // your code goes here
  System.out.println(unique());
 }

  public static boolean unique()
 {
    String str ="asdfghj";
    boolean arr[] = new boolean[256];
    for(int i=0;i < str.length();i++)
    {
      if(arr[i])
      {
          return false;
      }
      arr[i]=true;
    }
  return true;
 }

}
Solution 3 using array java(Worst case Complexity N2).

This is a simple iteration method that checks every alphabet with all the alphabet in the string to check if they match or not. If they matches then string is not unique.

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
 public static void main (String[] args) throws java.lang.Exception
 {
  // your code goes here
  System.out.println(unique());
 }

 public static boolean unique()
 {
    String str ="asdfghj";
    for(int i=0;i < str.length();i++)
    {
      char c = str.charAt(i);
      for(int j=i+1;j < str.length();j++)
      { 
          if(c==str.charAt(j))
          {
             return false;
          } 
      }
    }
    return true;
 }

}

Try more and more problems that you find on string and arrays and try to develop an approach the will help you understand problems rather then learning the solution. Once you know how to approach the problem you will able to solve any problem related to strings and array. I wish best of luck to you for your future.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

Blog Archive

  • ▼  2017 (18)
    • ▼  August (2)
      • Dart replacing the node in the abstract syntax tree
      • Dart Programming
    • ►  March (10)
    • ►  February (6)
  • ►  2016 (1)
    • ►  December (1)
  • ►  2013 (1)
    • ►  August (1)

Popular Posts

  • Arrays-String interview question 2
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the Cha...
  • How to print the node of the given linked list
    Linklist Interview Question How to print the node of the given linked list If you're new to linked lists, this is a great ex...
  • Dart Programming
    Dart Programming Hey guys i am providing the solutions for the some of the problems that i have faced and worked on while exploring t...
  • Arrays-String interview question 8
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the C...
  • Arrays-String interview question 4
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the Ch...
  • Arrays-String interview question 3
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the Ch...
  • Arrays-String interview question 6
    Cracking the coding interview Arrays and String solutions Hey guys i am providing the solutions for the question given in the Ch...
  • Arrays-String interview question 1
    Cracking the coding interview Arrays and String question solutions Hey guys i am providing the solutions for the question given i...
  • Array Left Rotation
    Array Interview Question Hacker Rank Question Array Left Rotation Hey guys i am providing the solutions for the question given in...
  • Dart replacing the node in the abstract syntax tree
    Dart replacing the node in the abstract syntax tree Here is the solution for that. import 'package:analyzer/analyzer.dart'...

Categories

  • about-scra
  • abstract-syntax-tree
  • array
  • array-interview-question
  • array-left-rotation
  • compare-two-linked-list
  • cracking-the-coding-interview
  • dart
  • dart-analyzer
  • flutter
  • frequently-asked-question
  • google-flutter
  • interview-question
  • java
  • left-rotation
  • linklist-interview-question
  • linklist-print-a-node
  • linklist-question
  • popular-linklist-question
  • popular-string-question
  • scra
  • scra-2018
  • string
  • string-interview-question
  • string-palindrome-question

About Me

Tech host
I am a blogger interested in programming and coding challenges. I am interested in knowing about the new technologies and new tech products that are launched daily and to write about them.
View my complete profile

Label

  • about-scra
  • abstract-syntax-tree
  • array
  • array-interview-question
  • array-left-rotation
  • compare-two-linked-list
  • cracking-the-coding-interview
  • dart
  • dart-analyzer
  • flutter
  • frequently-asked-question
  • google-flutter
  • interview-question
  • java
  • left-rotation
  • linklist-interview-question
  • linklist-print-a-node
  • linklist-question
  • popular-linklist-question
  • popular-string-question
  • scra
  • scra-2018
  • string
  • string-interview-question
  • string-palindrome-question
Powered by Blogger.
Revoltify

Revoltify

Copyright © Data Structure and Dart Programming language problems | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates