Monday, March 28, 2016

An optimised approach to find the second largest number in an array

An optimised approach to find the second largest number in an array.


There can be various solutions to this problem. I've listed three solutions which I found from the internet.



Solution 1:
int main()
{
 unsigned char arr[] = {13,10,2,1,5,9,7,6,19,14,13,15,19};
 int size = sizeof(arr)/sizeof(unsigned char);
 int i=0;
 unsigned char max = 0, sec = 0;//sec is used for the second largest no. in the array
 for(i=0;i<size;i++)
 {
  if(arr[i]>max)
  {
   sec = max;
   max=arr[i];
  }
  else if(arr[i]>sec && arr[i]!=max)
  {
   sec = arr[i];
  }
 }
 printf("max = %d\nsec = %d",max,sec);
 return 0;
}

This solution has a time complexity of ~O(n)


Solution 2:
To sort the array using the fastest algorithm viz. QuickSort: O(nLogn) and get the second largest.

The time of this technique O(nLogn) is still slower than O(n) therefore Solution 1 is preferable.


Solution 3:
In this technique the array is broken in groups of two and selection and elimination is done like a knock out tournament.

This technique has a time complexity of O(n + logn - 2)
Reference: http://stackoverflow.com/questions/3628718/find-the-2nd-largest-element-in-an-array-with-minimum-number-of-comparisons/3628777#3628777


My Solution:
int main()
{
 unsigned char arr[] = {13,10,2,1,5,9,7,6,19,14,13,15,19};
 int size = sizeof(arr)/sizeof(unsigned char);
 int i=0,j=0;
 unsigned char max = 0, sec = 0;//sec is used for the second largest no. in the array
 for(i=1;i<=size;i+=2)
 {
  j=(arr[i-1]>arr[i]);
  if(arr[i-j]>max)
  {
   sec = max;
   max=arr[i-j];
  }
  else if(arr[i-j]>sec && arr[i-j]<max)
  {
   sec = arr[i-j];
  }
 }
 printf("max = %d\nsec = %d",max,sec);
 return 0;
}

This technique shows a time complexity of ~O(n/2)


Please correct me, if we have a difference in opinion.


Thanks Ajitav :)

Wednesday, August 5, 2015

Interview Q&A


  1. Static Keyword in C++
    http://www.studytonight.com/cpp/static-keyword.php
  2. Vtable and VPtr [c++]
    http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/
  3. Introduction to SCRUM [video]
    http://scrummethodology.com/scrum-epics/
  4. Interface vs Abstract Class [java]
    http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/
  5. Java Reference Objects [java]
    http://www.kdgregory.com/?page=java.refobj
  6. Java Pass by reference or pass by value [java]
    http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value
  7. New vs Malloc [c++]
    http://electrofriends.com/qna/software-languages/c-cpp-faq/difference-malloc/
  8. Macro vs Inline function [c++]
    http://forums.codeguru.com/showthread.php?328273-C-General-What-are-the-differences-between-inline-functions-and-macros
  9. Stack vs Heap [c]
    http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html
    http://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.8.html
  10. Calling Conventions [c++]
    http://www.tenouk.com/Bufferoverflowc/bufferoverflowvulexploitdemo31.html
    http://www.codeproject.com/Articles/1388/Calling-Conventions-Demystified
  11. Pointers vs Reference [c++]
    https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp4_PointerReference.html
  12. Copy constructer [c++]
    https://en.wikipedia.org/wiki/Copy_constructor_(C%2B%2B)
  13. Deep copy vs Shallow copy Constructor [c++]
    http://stackoverflow.com/questions/2657810/deep-copy-vs-shallow-copy
  14. Why Virtual functions? [c++]
    http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-methods-in-c
  15. Object slicing [c++]
    http://stackoverflow.com/questions/274626/what-is-object-slicing#274636
  16. How to seal a class in c++ [c++]
    http://stackoverflow.com/questions/4712992/how-to-define-sealed-class-in-c
  17. Upcasting and downcasting [java]
    http://stackoverflow.com/questions/23414090/what-is-the-difference-between-up-casting-and-down-casting-with-respect-to-class
    http://www.bogotobogo.com/cplusplus/upcasting_downcasting.php [c++]
  18. Preorder, Inorder and Postorder
    http://stackoverflow.com/questions/9456937/when-to-use-preorder-postorder-and-inorder-binary-search-tree-traversal-strate
  19. Can a recursive function be inline?
    http://stackoverflow.com/questions/190232/can-a-recursive-function-be-inline
  20. Can virtual functions be inlined?
    http://www.geeksforgeeks.org/inline-virtual-function/
  21. Overloading Operators [c++]
    http://www.studytonight.com/cpp/operator-overloading-examples
  22. Virtual Destructor
    http://www.studytonight.com/cpp/virtual-destructors.php
  23. Memory Granularity [memory]
    https://developer.ibm.com/technologies/systems/articles/pa-dalign/


Thursday, June 25, 2015

Interview Helper

Embedded System
Embedded C Questions
C++
Java
Miscellaneous
GSM Test Specs

Wednesday, October 1, 2014

Debugging PHP applications with XDebug

Debugging any PHP application with XDebug is easy and can be configured very easily.

Step 1. Download Eclipse for PHP from the Eclipse website.

e.g.
Step 2. Now download Apache Webserver (Xampp is cool)


You can download a Xampp installer package or alternatively can also download a portable zip or a 7zip package.



Simply, install via installer or uncompress the package in your intended path.

Step 3. Now go to the path where Xampp is installed or unpacked. Open the file \xampp\php\php.ini

Reach to the end under the [XDebug] section and uncomment all the lines beneath.


Set xdebug.remote_enable = 1

Step 4. Download and Install Google Chrome or alternatively download portable version of Google Chrome. 

From Chrome's webstore install the Xdebug Helper plugin.


Step 5. Set the workspace for the Eclipse Projects as xampp\htdocs. Create a new project in PHP let's say Test.
So the path now is xampp\htdocs\Test
Create your own php project. I have shown a small program for example.


Step 6. To start Xampp setup via setup_xampp.bat from the Xampp folder. Then open the Xampp control panel and start the Apache and Mysql process.


Step 7. Enable the XDebug plugin by selecting the Debug option from the options as shown below.


Step 8. From the Eclipse IDE, go to Windows - Preferences - PHP - Debug - Installed Debuggers
Select the XDebug option and click Configure.
Select the Accept remote session (JIT) to localhost in the XDebug settings window and we're ready to go.


Step 9. From the Web browser, reload the php webpage you would like to debug.
Eclipse will then switch to the Debug perspective for you to debug.


Thanks for your patience ...